简体   繁体   中英

Event on notification bar like Status bar android

I created a Notification using Notification.FLAG_ONGOING_EVENT . Now I want to use it as on/off switch just like bluetooth or wifi in Status bar.

I want to use it as on off switch for my service. If I click it, it will start the service and if I click it again it will off the service. Just like the bluetooth /wifi or other things in status bar. As I can't put anything on Status bar I want to use the notification bar in that way. Is it possible?

Use a Pending intent that will launch your activty and put extras to know if u have to switch on/off.

// notification is clicked
Intent intent = new Intent(this, Main.class);
intent.putExtra(Main.SWITCH_ON, true);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);

Extras bundle on the oncreate method

Bundle extras = getIntent().getExtras();
        if(extras != null && extras.containsKey(SWITCH_ON) && extras.getBoolean(SWITCH_ON)) {
            launchMyMethod();
        }

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