当屏幕打开或关闭时,启动单个活动作为锁定屏幕

时间:2017-01-06 12:15:28

标签: android

当我的手机屏幕打开和关闭时,如何调用我的锁屏应用的单个活动(屏幕锁定), 我正在努力,但没有实现我的目标。请帮帮我,我很累。我从广播接收器调用活动,但是当应用程序关闭活动未启动时,此活动仅启动我的应用程序正在运行。 REciver是`

@Override
public void onReceive(Context context, Intent intent) { Toast.makeText(context, "BroadcastReceiver", Toast.LENGTH_SHORT).show();

    if (intent.getAction().equals(Intent.ACTION_SCREEN_OFF)) {
        Intent lockIntent = new Intent(context, Lockview.class);
        lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(lockIntent);

        Toast.makeText(context, "Screen is lock", Toast.LENGTH_SHORT).show();

    } else if (intent.getAction().equals(Intent.ACTION_SCREEN_ON)) {
        Toast.makeText(context, "Screen is unlocked", Toast.LENGTH_SHORT).show();
    } else if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
        Intent lockIntent = new Intent(context, Lockview.class);
        lockIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(lockIntent);
        Toast.makeText(context, "Screen is book", Toast.LENGTH_SHORT).show();

    }`

和MAnifest是`                                               

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <receiver
        android:name=".AEScreenOnOffReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.SCREEN_ON" />
            <action android:name="android.intent.action.SCREEN_OFF" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>`    and the activity where I trigger the Reciver is   ` BroadcastReceiver mybroadcast=new AEScreenOnOffReceiver();
    registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_ON));
    registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_SCREEN_OFF));

    registerReceiver(mybroadcast, new IntentFilter(Intent.ACTION_BOOT_COMPLETED));` in the oncreate 

1 个答案:

答案 0 :(得分:0)

我已经解决了我在屏幕打开时直接从广播接收器启动活动的问题。只有将我的Manifest接收器声明更改为<receiver android:name=".AEScreenOnOffReceiver" android:enabled="true"> <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <action android:name="android.intent.action.SCREEN_ON" /> <action android:name="android.intent.action.SCREEN_OFF" /> <category android:name="android.intent.category.HOME" /> <action android:name="android.intent.action.USER_PRESENT" /> <category android:name="android.intent.category.DEFAULT" /> </intent-filter> </receiver>