首页 > 解决方案 > 我的应用程序在屏幕解锁后无法启动

问题描述

我使用 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>

标签: javaandroidandroid-broadcastreceiver

解决方案


覆盖 android 活动或片段的 onsavedinstatestate 和 onrestor einstancestate 方法。


推荐阅读