【问题标题】:Launch a single activity as lock screen when the screen on or off当屏幕打开或关闭时,将单个活动作为锁定屏幕启动
【发布时间】:2017-01-07 02:16:16
【问题描述】:

当我的手机屏幕打开和关闭时,如何调用我的锁屏应用程序的单个活动(屏幕锁定), 我正在努力但没有达到我的目标。请帮帮我,我累了。我从广播接收器调用活动,但是当应用程序关闭活动不启动时,这只会启动我的应用程序正在运行的活动。 接收器是`

@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 

【问题讨论】:

  • 我已经编辑了我的帖子,请帮忙
  • 您是否在 mainfest 中添加了这些? &lt;uses-permission android:name="android.permission.WAKE_LOCK" /&gt;&lt;uses-permission android:name="android.permission.DISABLE_KEYGUARD" /&gt;
  • 是的,我有
  • 先生,我看到了,我的问题是当应用程序未运行时我无法启动我的活动。当运行我的广播接收器的应用程序工作时。

标签: android


【解决方案1】:

我已经解决了当屏幕打开时直接从广播接收器将活动作为屏幕锁定启动的问题。只需将我的 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>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-31
    • 2021-04-01
    • 2017-05-27
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多