java - 我的应用程序在屏幕解锁后无法启动
问题描述
我使用 BroadcastReciver 来接收用户是否解锁屏幕或重启手机(用户在场)并启动活动,但是当我解锁屏幕时没有任何效果。我的想法是获取用户状态并像弹出广告商一样多次启动我的应用程序
这是我的接收器代码:
public class recieve extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equalsIgnoreCase(Intent.ACTION_BOOT_COMPLETED) || intent.getAction().equalsIgnoreCase(Intent.ACTION_SCREEN_ON) || intent.getAction().equalsIgnoreCase(Intent.ACTION_USER_PRESENT)) {
Intent serviceIntent = new Intent(context, serviceonboot.class);
context.startService(serviceIntent);
}
}
}
这是用户在场时启动的服务
public class serviceonboot extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
// here you can add whatever you want this service to do
startActivity(new Intent(this,BreakActivity.class));
}
}
这是我的清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zsoft.john.eyeguard">
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<application
android:allowBackup="true"
android:icon="@drawable/eyeguard"
android:label="Eye Guard"
android:roundIcon="@drawable/eyeguard"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".BreakActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver
android:name="com.zsoft.john.eyeguard.recieve"
android:enabled="true"
android:exported="false">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SCREEN_ON" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SCREEN_OFF" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.USER_PRESENT" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REPLACED" />
<data android:scheme="package" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.PACKAGE_ADDED" />
<data android:scheme="package" />
</intent-filter>
</receiver>
<service android:name="com.zsoft.john.eyeguard.serviceonboot"></service>
</application>
</manifest>
解决方案
覆盖 android 活动或片段的 onsavedinstatestate 和 onrestor einstancestate 方法。
推荐阅读
- web3js - Web3 批量请求返回未定义
- node.js - Chrome 正在调用两次返回 mp3 文件的路由
- javascript - 使用块外的值时返回 null
- aws-lambda - Lambda 链接 - 使用 terraform 从另一个 lambda 调用 lambda
- c++ - Poco c++ 如何从 PostgreSQL DB 中读取“文本”数据类型?
- c# - 将每个列单元格值与一行中的下一个列单元格值进行比较
- php - 是否有任何选项可以根据 if 条件使用 eloquent 与关系?
- c - 如何将存储在另一个指针指向的地址的值分配给指向指针的地址?
- css - 如何将按钮与页面底部对齐
- javascript - 如何从外部 url (json) 读取数据并在另一个 html 页面中使用 javascript 打印它?