有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

我的代码:

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(安卓.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();
    }


}

共 (0) 个答案