How to Start one Activity and two service on notification click

人走茶凉 提交于 2019-12-11 02:29:08

问题


When user click on notification I want to start an Activity and two Intent Service.

Is it possible to do ? If yes Please can any one give me the idea to do this ?

Suppose I have no control on Activity. The Activity belongs to third party app.

Please don't tell me that start intent service from your Activity . I know this.


回答1:


Follow the steps Hope this helps you

1) Create a BroadCastReceiver

public class MyBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        // Start Activity
        // Start Services
    }

}

2) Just Fire Broadcast on notification click.

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        Intent intent = new Intent(context, MyBroadcastReceiver.class);
        PendingIntent contentIntent = PendingIntent.getBroadcast(context, 0, intent, 0);
        Notification notification = new Notification(icon, ticker, when);
        notification.setLatestEventInfo(mSmartAndroidActivity, title, message, contentIntent);
        notification.flags = Notification.FLAG_AUTO_CANCEL;
        notificationManager.notify(0, notification);

Use PendingIntent.getBroadcast instead of PendingIntent.getActivity

3) In BroadcastReceiver's onReceive() method

-> call your third party activity

-> Start services




回答2:


The answer given by Biraj will also do the job.

But for that You have to declare the BroadcastReceiver in your manifest file as well as all activity and services which you are starting from BroadcastReceiver.

So I have not choosen the above way.

How I have implemented ?

I have started an Intent service from where I am starting activity and making a synchronous request to server . By this way I don't have to declare a BroadcastReceiver in my maniest file.

I believe in writing less code so I have chosen the above Intent Service way



来源:https://stackoverflow.com/questions/23651982/how-to-start-one-activity-and-two-service-on-notification-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!