Reputation: 2021
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.
Upvotes: 2
Views: 1551
Reputation: 2021
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
Upvotes: 0
Reputation: 28484
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
Upvotes: 3