calling a method in service after click on notification
You can specify a Broadcast
in your Service
and launch that via a PendingIntent
in your notification.
Intent stopIntent = new Intent("my.awersome.string.name");
PendingIntent stopPi = PendingIntent.getBroadcast(this, 4, stopIntent, PendingIntent.FLAG_CANCEL_CURRENT);
Then, in your notification builder:
NotificationCompat.Builder builder;
builder = new NotificationCompat.Builder(context)
...
.setContentIntent(stopPi);
In your Service
you can setup a BroadcastReceiver
as:
private final BroadcastReceiver myReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Code to run
}
};
You register this receiver
in your Service
(possibly in onStartCommand()
) only using:
registerReceiver(myReceiver, new IntentFilter("my.awersome.string.name"));
Your Service
MUST be running for this to work.
How to call a non-activity method on Notification Click
You could do something like this:
When creating your PendingIntent
to put in the Notification
:
Intent notificationIntent = new Intent(MainActivity.this, MyClass.class);
notificationIntent.putExtra("fromNotification", true);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
Now, in MyClass.onCreate()
:
if (getIntent().hasExtra("fromNotification")) {
callMethod();
}
Execute an action on notification click
You'll have to use a BroadcastReceiver
for that. Take a look at the following code. Put it in your Service
private MyBroadcastReceiver mBroadcastReceiver;
@Override
onCreate() {
super.onCreate();
mBroadcastReceiver = new MyBroadcastReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addCategory(Intent.CATEGORY_DEFAULT);
// set the custom action
intentFilter.addAction("do_something");
registerReceiver(mBroadcastReceiver, intentFilter);
}
// While making notification
Intent i = new Intent("do_something");
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, i, 0);
notification.contentIntent = pendingIntent;
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
switch(action) {
case "do_something":
doSomething();
break;
}
}
}
public void doSomething() {
//Whatever you wanna do on notification click
}
This way the doSomething()
method will be called when your Notification
is clicked.
How to invoke a method on a foreground service by clicking on a notification action?
Define a constant:
private const val EXTRA_STOP = "stop";
Then create an intent for your service and put an extra flag:
val intent = Intent(context, YourService::class.java);
intent.putExtra(EXTRA_STOP, true);
Now you can create a pending intent as your handler:
val pendingIntent: PendingIntent = PendingIntent.getService(context, YOUR_REQUEST_CODE, intent, PendingIntent.FLAG_IMMUTABLE)
This pending intent will trigger the onStartCommand
method on your service, where you can check whether the stop flag was set.
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
if (intent != null && intent.getBooleanExtra(EXTRA_STOP, false)) {
stopAction()
}
...
}
Android call method on notification click
Add android:launchMode="singleTop"
in your activity
in your manifest file, have the method protected void onNewIntent(Intent intent) { ... }
and use this code:
private static final int MY_NOTIFICATION_ID = 1;
private NotificationManager notificationManager;
private Notification myNotification;
void notification() {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
myNotification = new Notification(R.drawable.next, "Notification!", System.currentTimeMillis());
Context context = getApplicationContext();
String notificationTitle = "Exercise of Notification!";
String notificationText = "http://android-er.blogspot.com/";
Intent myIntent = new Intent(this, YourActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(YourActivity.this, 0, myIntent, Intent.FILL_IN_ACTION);
myNotification.flags |= Notification.FLAG_AUTO_CANCEL;
myNotification.setLatestEventInfo(context, notificationTitle, notificationText, pendingIntent);
notificationManager.notify(MY_NOTIFICATION_ID, myNotification);
}
Related Topics
Android Multiple Selection Listview & Textview
Picasso Illegalargumentexception Target Must Not Be Null
How to Use Startactivityforresult() for Email Intent
Show Toast Widget Underneath a View
Android: How to Programmatically Set the Size of a Layout
Does Setwidth(Int Pixels) Use Dip or Px
Gridview VS Gridlayout in Android Apps
Trying to Add Adb to Path Variable Osx
How to Make a Edittext Box in a Dialog
Detect If an App Is Installed from Play Store
Android SQLite Database, Why Drop Table and Recreate on Upgrade
Android Calling Asynctask Right After an Another Finished
How to Change the Color of the Tabs Indicator Text in Android
Convert Xml to JSON Object in Android
Android: Launch Activity from Clickable Text