应用关闭时,广播接收器无法正常工作它在应用程序运行时运行良好

时间:2017-05-06 12:54:16

标签: android android-view lockscreen homescreen

该应用程序的目的是在电源连接或断开时在主屏幕上显示视图,而不管应用程序的运行状态如何。即使应用程序没有运行,它也应该在主屏幕上显示视图。 的问题: 1:应用程序关闭时,没有显示弹出屏幕。

注意 :在应用运行时会显示一个弹出屏幕。

设备:Lollipop

模式:开发者模式

我已附上代码。 主要的Activity类如下。它在应用程序开始时打开。

public class MainActivity extends AppCompatActivity {

private static final int REQUEST_CODE_SOME_FEATURES_PERMISSIONS = 101;
static WindowManager.LayoutParams params;
static WindowManager wm;
static ViewGroup lockScreen;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Bundle bundle = getIntent().getExtras();
    try {
        String from = bundle.getString("from");
        if (from.equalsIgnoreCase("broadcast")) {
            startActivity(new Intent(getApplicationContext(), BatteryAdActivity.class));
            finish();

        }
    } catch (Exception ex) {

    }

    getPermission();


}

private void getPermission() {
    String[] PERMISSIONS = {Manifest.permission.BATTERY_STATS, Manifest.permission.SYSTEM_ALERT_WINDOW, Manifest.permission.REORDER_TASKS};
    if (!hasPermissions(this, PERMISSIONS)) {
        ActivityCompat.requestPermissions(this, PERMISSIONS, REQUEST_CODE_SOME_FEATURES_PERMISSIONS);
    }
}

public static boolean hasPermissions(Context context, String... permissions) {
    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context != null && permissions != null) {
        for (String permission : permissions) {
            if (ActivityCompat.checkSelfPermission(context, permission) != PackageManager.PERMISSION_GRANTED) {
                return false;
            }
        }
    }
    return true;
}

}

2:BroadCast接收器类如下。

public class PowerConnectionReceiver extends BroadcastReceiver {
Context context;

@Override
public void onReceive(Context context, Intent intent) {
    this.context = context;
    PackageManager pm = context.getPackageManager();
    Intent launchIntent = pm.getLaunchIntentForPackage("com.dogpo.batteryoptimize");
    launchIntent.putExtra("from", "broadcast");
    context.startActivity(launchIntent);

    showMessage("charging");

}

private void showMessage(String charging) {
    Toast.makeText(context, charging, Toast.LENGTH_SHORT).show();
}

}

3:电源断开或连接时打开的活动如下。

public class BatteryAdActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_battery_ad);
}

}

4:清单代码如下。

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.BATTERY_STATS" />
<uses-permission android:name="android.permission.REORDER_TASKS" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <receiver android:name=".PowerConnectionReceiver">
        <intent-filter>
            <action android:name="android.intent.action.ACTION_POWER_CONNECTED" />
            <action android:name="android.intent.action.ACTION_POWER_DISCONNECTED" />
        </intent-filter>
    </receiver>

    <activity android:name=".BatteryAdActivity"
        ></activity>
</application>

1 个答案:

答案 0 :(得分:-1)

在您要显示广告的位置声明一个明确大小的空白相对布局。 现在:

var i