Вы не можете напрямую вызывать методы при нажатии кнопок действий.
Чтобы выполнить это, вам необходимо использовать PendingIntent с BroadcastReceiver или Service. Вот пример PendingIntent с BroadcastReciever.
Первая позволяет создавать уведомления,
public static void createNotif(Context context){
...
//This is the intent of PendingIntent
Intent intentAction = new Intent(context,ActionReceiver.class);
//This is optional if you have more than one buttons and want to differentiate between two
intentAction.putExtra("action","actionName");
pIntentlogin = PendingIntent.getBroadcast(context,1,intentAction,PendingIntent.FLAG_UPDATE_CURRENT);
drivingNotifBldr = (NotificationCompat.Builder) new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.steeringwheel)
.setContentTitle("NoTextZone")
.setContentText("Driving mode it ON!")
//Using this action button I would like to call logTest
.addAction(R.drawable.smallmanwalking, "Turn OFF driving mode", pIntentlogin)
.setOngoing(true);
...
}
Теперь приемник, который получит этот Намерение
public class ActionReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Toast.makeText(context,"recieved",Toast.LENGTH_SHORT).show();
String action=intent.getStringExtra("action");
if(action.equals("action1")){
performAction1();
}
else if(action.equals("action2")){
performAction2();
}
//This is used to close the notification tray
Intent it = new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
context.sendBroadcast(it);
}
public void performAction1(){
}
public void performAction2(){
}
}
Объявить вещательный приемник в манифесте
<receiver android:name=".ActionReceiver"></receiver>
Надеюсь, что это поможет.
Спасибо, это имеет большой смысл. Тем не менее, я немного смущен, почему вы передали 'intentAction' в' addAction'. Будет ли это «pIntentlogin»? –
Обновлено. Виноват! :) –
Большое вам спасибо! –