第二个广播接收器意图永远不会被捕获到服务中

时间:2014-03-23 09:10:06

标签: android service broadcastreceiver screen

我正在尝试从正在运行的前台服务(下面的代码)中捕获以下两个广播(android.intent.action.SCREEN_ON& android.intent.action.SCREEN_OFF)。

仅限" android.intent.action.SCREEN_ON"被抓获了。 if(intent.getAction()。equals(" android.intent.action.SCREEN_OFF")) inReceive()永远不会被触发... myBroadcast_screenoff也没有。

我在这里做错了什么?

(基本问题是如何从单个服务捕获两个不同的意图)

public class ServiceDataSaver extends Service {

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}


BroadcastReceiver myBroadcast = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

        if(intent.getAction().equals("android.intent.action.SCREEN_ON")) {
            Log.v("datasaver", "Screen just turned on");
        }

        if(intent.getAction().equals("android.intent.action.SCREEN_OFF")) {
            Log.v("datasaver", "Screen just turned off");
        }
    }
};

BroadcastReceiver myBroadcast_screenoff = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {


        if(intent.getAction().equals("android.intent.action.SCREEN_OFF")) {
            Log.v("datasaver", "Screen just turned off");
        }
    }
};



@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();

    IntentFilter filter = new IntentFilter("android.intent.action.SCREEN_ON");
    filter.setPriority(999);
    registerReceiver(myBroadcast, filter);


    IntentFilter filter_screenoff = new IntentFilter("android.intent.action.SCREEN_ON");
    filter_screenoff.setPriority(999);
    registerReceiver(myBroadcast_screenoff, filter_screenoff);      
}

@Override
public void onDestroy() {
    super.onDestroy();
    // Do not forget to unregister the receiver!!!
    //this.unregisterReceiver);
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    //return super.onStartCommand(intent, flags, startId);


    makeServiceForeground(intent);
    return START_NOT_STICKY;        
}

public void makeServiceForeground(Intent receivedIntent) {
    final int myID = 1234;

    // The intent to launch when the user clicks the expanded notification
    Intent returningintent = new Intent(this, MainActivitySlidingLayer.class);
    receivedIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
            | Intent.FLAG_ACTIVITY_SINGLE_TOP);
    PendingIntent pendIntent = PendingIntent.getActivity(this, 0,
            returningintent, 0);

    // This constructor is deprecated. Use Notification.Builder instead
    Notification notice = new Notification(R.drawable.ic_launcher,
            "Facebook is Blocked", System.currentTimeMillis());

    // This method is deprecated. Use Notification.Builder instead.
    notice.setLatestEventInfo(this, "Facebook is blocked",
            "Click here to unblock it ...", pendIntent);

    notice.flags |= Notification.FLAG_NO_CLEAR;
    startForeground(myID, notice);
}

}

1 个答案:

答案 0 :(得分:1)

可能只是错字。您没有注册SCREEN_OFF意图。

在OnCreate()

  IntentFilter filter_screenoff = new IntentFilter("android.intent.action.SCREEN_ON");

应该是

  IntentFilter filter_screenoff = new IntentFilter("android.intent.action.SCREEN_OFF");

当然,您最好还是使用Intent.ACTION_SCREEN_OFFIntent.ACTION_SCREEN_ON提供的常量。