简体   繁体   中英

Launching an android app when phone connected to power source

I am trying to write an android app that should be launched, whenever the phone with the app is plugged to the AC power or connected through usb cord to, say a laptop. The behavior i want is that even when the android app is closed, once i plugged the phone to the power source, the app should launch and come to view.

i added the following to the AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.chargeit">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

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

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

I also added this to MainActivity

class OnPowerReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent?) {
        val i = Intent(context, MainActivity::class.java)
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        context.startActivity(i)
    }
}

However, when i ran and connect the device to power, the android app did not launch(come up). I would have preferred to code this solution using flutter but i learnt that flutter might not be a strong candidate for that level of interaction with phone hardware. Please what am i doing wrong? Any help will be greatly appreciated. Thanks

I think android.intent.action.ACTION_POWER_CONNECTED is not exempted from background broadcast list

you must explicitly register for it from your Java code to make it work.

and as part of the Android 8.0 (API level 26) Background Execution Limits, apps that target the API level 26 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest. However, several broadcasts are currently exempted from these limitations. Apps can continue to register listeners for the following broadcasts, no matter what API level the apps target.

Please refer to below documentation for your broadcast too -

broadcast

You can register like below -

private BroadcastReceiver receiver;

protected void onResume() {
 super.onResume();



  IntentFilter filter = new IntentFilter();
  filter.addAction("android.intent.action.ACTION_POWER_CONNECTED");


  receiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {

    }
  };
     registerReceiver(receiver, filter);
}

And to unregister -

 @Override
 protected void onDestroy() {
  if (receiver != null) {
   unregisterReceiver(receiver);
   receiver = null;
  }
  super.onDestroy();
 }

In my project, I can't receive any event when I only set intentFilter. But If I set a high priority to intentFilter, I receive the power connected or disconnected event every time(I test with an android 11 device):

    val intentFilter = IntentFilter()
    intentFilter.addAction(Intent.ACTION_POWER_CONNECTED)
    intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED)
    intentFilter.priority = IntentFilter.SYSTEM_HIGH_PRIORITY
    registerReceiver(PowerConnectionReceiver(), intentFilter)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM