Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在非静态服务类中使用广播接收机_Java_Android_Broadcastreceiver_Android Service - Fatal编程技术网

Java 如何在非静态服务类中使用广播接收机

Java 如何在非静态服务类中使用广播接收机,java,android,broadcastreceiver,android-service,Java,Android,Broadcastreceiver,Android Service,你好,我在服务类中创建了广播接收器来接收应用程序通知,但它不接收来自通知的任何意图。当我将广播接收器设置为静态时,问题就解决了,但此时我无法访问非静态上层类的元素。我必须解决这个问题,不要让它静止 我的代码: public class BackgroundService extends Service { private final int TASK_DELAY = 0; private final int TASK_PERIOD = 5 * 1000; int NO

你好,我在服务类中创建了广播接收器来接收应用程序通知,但它不接收来自通知的任何意图。当我将广播接收器设置为静态时,问题就解决了,但此时我无法访问非静态上层类的元素。我必须解决这个问题,不要让它静止

我的代码:

public  class BackgroundService extends Service {

    private final int TASK_DELAY = 0;
    private final int TASK_PERIOD = 5 * 1000;
    int NOTIFICATION_ID = 1;
    private Context context;
    private NotificationCompat.Builder builder;
    private NotificationManager notificationManager;
    private static Timer timer;
    private PendingIntent test;
    private int runRate;


    public class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            //User pressed a notifiacition button
            Log.w(TAG, "onReceive: Recived" );
        }

        // constructor
        public MyReceiver(){

        }
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }

    public static Timer getTimer() {
        return timer;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }


    @Override
    public void onCreate() {
        Toast.makeText(this, "Service has been started!", Toast.LENGTH_SHORT).show();
        context = getApplicationContext();
        timer = new Timer();
        runRate = 0;



        builder = new NotificationCompat.Builder(context)
                .setSmallIcon(android.R.drawable.ic_dialog_alert)
                .setContentTitle("KolBoost")
                .setContentText("Arkaplan servisi etkinleştirildi!")
                .setAutoCancel(false)
                .setPriority(NotificationCompat.PRIORITY_HIGH);

        MyReceiver myReceiver = new MyReceiver();
        IntentFilter filter = new IntentFilter();

        Intent close = new Intent(getBaseContext(), BackgroundService.class);
        close.setAction("CLOSE_SERVICE");
        PendingIntent closeServiceIntent = PendingIntent.getBroadcast(getBaseContext(), 0, close, 0);

        Intent i2 = new Intent(getBaseContext(), BackgroundService.class);
        i2.setAction("BOOST_MEMORY");
        PendingIntent boostIntent = PendingIntent.getBroadcast(getBaseContext(), 0, i2, 0);

        Intent launch = new Intent(getBaseContext(),BackgroundService.class);
        launch.setAction("OPEN_MANAGER");

        PendingIntent contentIntent = PendingIntent.getBroadcast(getBaseContext(), 0, launch, 0);

        builder.setContentIntent(contentIntent);

        builder.addAction(0, "Clear Memory", boostIntent);
        builder.addAction(0, "Exit", closeServiceIntent);


        notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        Intent notificationIntent = new Intent(getBaseContext(), MainActivity.class);
        test = PendingIntent.getActivity(getBaseContext(), NOTIFICATION_ID, notificationIntent, PendingIntent.FLAG_NO_CREATE);

        //I'm adding actions to intentFilter.
        filter.addAction(close.getAction());
        filter.addAction(i2.getAction());
        filter.addAction(launch.getAction());
        //Registering Receiver with intentFilter
        registerReceiver(myReceiver,filter);




        super.onCreate();
    }

    @Override
    public void onDestroy() {
        timer.cancel();
        notificationManager.cancelAll();
        Log.d(TAG, "onDestroy: Destroyed");
        super.onDestroy();
    }


}

“不起作用”不是一个足够的问题描述。什么不起作用?你有什么例外吗?@QBrute它从通知中没有任何意图,我不知道我做错了什么。