首页 > 解决方案 > 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;
    }
}

标签: javaandroidservicenotificationsparameter-passing

解决方案


推荐阅读