简体   繁体   中英

How to show nottification when (andorid) home button is pressed (background)

i have application which send nottification in every Chrono.change (testing purposes for now). Its working fine when im on MainActivity where the function was developed. But its not working on any other activities OR when the HOME button is pressed. If the POWER button is pressed applications run on background and its working fine (if POWER button is pressed on MainActivity).

Any idea how to solve this two issues:

1) Notification is send also if im on another activity

2) more critical - Notification is send even if the HOME button is pressed and im not currently in app.

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);
    }

Thanks a milion!

Answer by @Raghunandan

It is not possible.

There is no way to intercept the home button on Android, unless you make your app the home screen. This is for security reasons, so that malicious apps cannot take over your device by overriding all the buttons that can exit.

If you want to handle the HOME button, implement a home screen.

But I would suggest you to rethink your design. Leave the option of logout to the user.

http://developer.android.com/training/design-navigation/index.html

Look at the gmail app. When you press home button it does not logout.

@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(); 
 }

You can use onUserLeaveHint

protected void onUserLeaveHint()

Added in API level 3

Called as part of the activity lifecycle when an activity is about to go into the background as the result of user choice. For example, when the user presses the Home key, onUserLeaveHint() will be called, but when an incoming phone call causes the in-call Activity to be automatically brought to the foreground, onUserLeaveHint() will not be called on the activity being interrupted. In cases when it is invoked, this method is called right before the activity's onPause() callback.

This callback and onUserInteraction() are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notfication.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM