屏幕关闭时启动应用程序

时间:2013-01-21 09:18:27

标签: android screen

我想在屏幕关闭时打开我的MainActivity类。为了做到这一点,我做了两节课 ScreenReceiver.java用于处理Screen OFF&屏幕ON意图:

public class ScreenReceiver extends BroadcastReceiver {

 private boolean screenOff;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            screenOff = true;
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            screenOff = false;
        }
        Intent i = new Intent(context, UpdateService.class);
        i.putExtra("screen_state", screenOff);
        context.startService(i);
    }

}

用于实现ScreenReceiver的UpdateService:

public  class UpdateService extends Service {

@Override
public void onCreate() {
    super.onCreate();
    // register receiver that handles screen on and screen off logic
    IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
    filter.addAction(Intent.ACTION_SCREEN_OFF);
    BroadcastReceiver mReceiver = new ScreenReceiver();
    registerReceiver(mReceiver, filter);
}

public void onStart(Context context, Intent intent, int startId) {
    boolean screenOn = intent.getBooleanExtra("screen_state", false);
    if (!screenOn) {
        // your code
         Intent intent11 = new Intent(context,MainActivity.class);
         intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         context.startActivity(intent11);
    } else {
        // your code
        Intent intent11 = new Intent(context,MainActivity.class);
        intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    }
}

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

}

因此,当我安装我的应用程序时,屏幕关闭时没有任何事件。请告诉我这个问题。

2 个答案:

答案 0 :(得分:0)

您是否在应用程序的某个位置启动了UpdateService?

答案 1 :(得分:0)

首先,意图ACTION_SCREEN_OFFACTION_SCREEN_ON只能由通过函数registerReceiver()注册的接收者处理。在manifest.xml中定义IntentFilter不适用于这些意图。

然后,您需要确保在您的应用程序中调用UpdateService:onCreate(),否则永远不会调用ScreenReceiver:onReceiver()。获得意图BOOT_COMPLETED时,您可能希望这样做。

您可以将代码更改为此,并且不要忘记在清单中定义服务:

public class UpdateService extends Service {

    BroadcastReceiver mReceiver = new BroadcastReceiver {
    private boolean screenOff;

    @Override
    public void onReceive(Context context, Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
            screenOff = true;
        } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
            screenOff = false;
        }
        handleScreenAction(screenOff);
    }

    private void handleScreenAction(boolean screenOff) {
        if (screenOff) {
            // your code
            Intent intent11 = new Intent(context,MainActivity.class);
            intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent11);
        } else {
            // your code
            Intent intent11 = new Intent(context,MainActivity.class);
            intent11.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        // register receiver that handles screen on and screen off logic
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        registerReceiver(mReceiver, filter);
    }

    @Override
    public void onDestory() {
        super.onDestory();
        unRegisterReceiver(mReceiver);
    }

    public void onStart(Context context, Intent intent, int startId) {
    }

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