java - Android - 从/到服务的通知按钮
问题描述
我创建了一个正在运行的服务,它通过一个按钮生成一个通知。我想在服务中创建一个侦听器,以便在按下按钮时执行某些操作。通知很简单,带有一个 addaction 语句。我成功地将数据发送到外部广播接收器,但本地接收器没有运气。我只是想从通知中更改服务的布尔值。
谢谢
public class TrackerService extends Service {
private static final int REQUEST_RESULT = 200;
final static String PANIC_ACTION = "PANIC";
private Point point = new Point();
private static final String TAG = "TrackerService";
private static final String NOTIFICATION_CHANNEL_ID = "tracker_service.channel";
private IntentFilter panicIntentFilter;
protected final IBinder binder = new TrackerServiceBinder();
public BroadcastReceiver trackerServiceBroadcastReceiver;
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
startMyOwnForeground();
else
startForeground(1, new Notification());
panicIntentFilter = new IntentFilter();
panicIntentFilter.addAction("PANIC");
this.registerReceiver(trackerServiceBroadcastReceiver, panicIntentFilter);
}
public class trackerServiceBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.d(TAG + " TSRECEIVER","Complete intent: " + intent.toString() + " extras intent: " + intent.getExtras().toString());
if (intent.getAction().equals("PANIC") && intent.hasExtra("PANIC")){
if (intent.getExtras().getBoolean("PANIC")) {
//point.setPanicState(true);
Log.d(TAG + " TSRECEIVER", "panic pressed - point.getPanic(): " + "true");
} else {
//point.setPanicState(false);
Log.d(TAG + " TSRECEIVER", "panic pressed - point.getPanic(): " + "false");
}
}
}
}
public class TrackerServiceBinder extends Binder {
public TrackerService getServiceInstance() {
return TrackerService.this;
}
}
public Boolean TogglePanicState() {
point.setPanicState( !point.getPanicState() );
return point.getPanicState();
}
@RequiresApi(Build.VERSION_CODES.O)
private void startMyOwnForeground() {
Intent panicIntent = new Intent(this, trackerServiceBroadcastReceiver.class); // Prueba al broadcaster local
//Intent panicIntent = new Intent(this, TrackerService.class); // Prueba al servicio
//Intent panicIntent = new Intent(this, NotificationReceiver.class); // Prueba a un Broadcaster externo - Funciona
panicIntent.setAction("PANIC");
panicIntent.putExtra("PANIC", true);
PendingIntent panicPendingIntent = PendingIntent.getBroadcast(this, 123456, panicIntent, PendingIntent.FLAG_UPDATE_CURRENT);
Log.d(TAG + " Notification","Complete intent: " + panicIntent.toString() + " extras intent: " + panicIntent.getExtras().toString() + " Complete PendingIntent: " + panicPendingIntent.toString());
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, "Tracker Service - Channel", NotificationManager.IMPORTANCE_NONE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.drawable.ou)
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.addAction(R.drawable.alarm, "PANIC", panicPendingIntent)
.build();
startForeground(1, notification);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
super.onStartCommand(intent, flags, startId);
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(trackerServiceBroadcastReceiver);
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
}
解决方案
推荐阅读
- javascript - 是否有任何已知的方法可以将字符串或数据从 html 页面发送到 c++ 应用程序?
- javascript - 在 ReactJS 中使用 npm XLSX 在工作簿中添加多个工作表
- python - 如何获得本周星期一 6 点 30 AM 时间?
- c# - 在 VS 2019 中使用带有项目模板的向导:向导不起作用
- redis - 在 StackExchange.Redis 中为多个端点/服务器使用 LoadedLuaScript
- python - python中给csv添加排名列的问题
- python - 如何在 pyspark 中使用 map 和 reduce 来处理带有一些列表变量的列表对象?
- python - Python将\ n转换为换行符而不打印?需要传递给另一个程序但希望 \n 转换
- javascript - 地图加载时如何在点击时设置标记 - Mapbox GLJS
- java - 如何将 Java 项目正确导入我的工作区并使其运行?