当(andorid)主页按钮被按下时(背景)如何显示注释
我有在每个Chrono.change(目前测试目的)发送批注的应用程序。它的工作很好,当我在MainActivity的功能开发。但是它不适用于任何其他活动或者当按下HOME按钮时。如果按下POWER按钮,应用程序将在后台运行并且工作正常(如果在MainActivity上按POWER按钮)。当(andorid)主页按钮被按下时(背景)如何显示注释
任何想法如何解决这个两个问题:
1)通知还发送,如果在另一个活动
2)更关键的IM - 通知是即使按HOME键发送和林不目前在应用中。
eventL istener:
stopWatch.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
turnedOnOff = prefs.getBoolean("notification",false);
if (turnedOnOff)
throwNotification();
}
});
Nottifications:
public void throwNotification()
{
// Prepare intent which is triggered if the
// notification is selected
Intent intent = new Intent(this, MainActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
intent, 0);
// Build notification
// Actions are just fake
Notification noti = new Notification.Builder(this)
.setContentTitle("Finish!")
.setContentText("Its done").setSmallIcon(R.mipmap.notif)
.setContentIntent(pendingIntent)
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
// hide the notification after its selected
noti.flags |= Notification.FLAG_AUTO_CANCEL;
noti.defaults |= Notification.DEFAULT_SOUND;
noti.defaults |= Notification.DEFAULT_VIBRATE;
notificationManager.notify(0, noti);
}
感谢畅想!
通过@Raghunandan回答
这是不可能的。
除非您将应用程序设置为主屏幕,否则无法拦截Android上的主页按钮。这是出于安全原因,因此恶意应用无法通过覆盖可退出的所有按钮来接管设备。
如果要处理HOME按钮,请实施主屏幕。
但我建议你重新考虑你的设计。将注销选项留给用户。
http://developer.android.com/training/design-navigation/index.html
看看Gmail应用。当您按Home键时,它不会注销。
@Override
protected void onUserLeaveHint()
{
super.onUserLeaveHint();
Toast.makeText(this, "You pressed the home button!", Toast.LENGTH_LONG).show();
Log.i("Home button Pressed!", "Yes");
finish();
}
您可以使用onUserLeaveHint
保护无效onUserLeaveHint()
在API级别3
调用作为活动周期的一部分,当活动即将进入后台为用户选择的结果。例如,当用户按下主页键时,onUserLeaveHint()将被调用,但当来电打电话导致通话中的Activity被自动带到前台时,onUserLeaveHint()将不会被中断的活动调用。在被调用的情况下,该方法在活动的onPause()回调之前被调用。
此回调和onUserInteraction()旨在帮助活动智能地管理状态栏通知;特别是帮助活动确定取消通知的适当时间。
感谢您的回复,螺母我不需要重新编程主页按钮。我只是需要我的应用程序能够发送通知,即使主页按钮被按下,我目前不在应用程序 –
@jan我的答案包括如何做到这一点。 – GoogleMac