BroadcastReceiver's behaviour for ACTION_SHUTD

2019-08-06 00:07发布

问题:

I have BroadcastReceiver which listens for ACTION_SHUTDOWN and some other actions.

My problem is, when I shutdown my android device(2.3.6) BroadcastReceiver is catching ACTION_SHUTDOWN two times. Problem is only with ACTION_SHUTDOWN and not with other actions.

When I run same code on emulator, it works fine.
Guys please help me. Here is my code:

my BootReceiver.java

public class BootReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

         if(Intent.ACTION_SHUTDOWN.equalsIgnoreCase(intent.getAction())) {
               Log.i("Boot Receiver - Shutdown event");
               // database operation
         }

         if(Intent.ACTION_BOOT_COMPLETED.equalsIgnoreCase(intent.getAction())) {
               // some operation
         }

         if(Intent.ACTION_AIRPLANE_MODE_CHANGED
                               .equalsIgnoreCase(intent.getAction().intern())) {
               // some operation
         }

         if(ConnectivityManager.CONNECTIVITY_ACTION
                                        .equalsIgnoreCase(intent.getAction())) {
               // some operation
         }
     }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xyz.ui"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>

<application
    android:allowBackup="true"
    android:icon="@drawable/spota"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" 
    >
    <activity
        android:name="com.xyz.UserActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Black.NoTitleBar.Fullscreen">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <receiver 
        android:name="com.xyz.BootReceiver">
         <intent-filter >
             <action android:name="android.intent.action.BOOT_COMPLETED" />
             <action android:name="android.intent.action.ACTION_SHUTDOWN" />
             <action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
             <action android:name="android.intent.action.BATTERY_CHANGED" />
             <action android:name="android.intent.action.AIRPLANE_MODE"/>
         </intent-filter>
    </receiver>

</application>

And here is the Logcat entries

05-03 16:37:23.826: I/xyz(2337): Boot Receiver - Shutdown event
05-03 16:37:23.881: I/xyz(2337): Inserting Data
05-03 16:37:28.514: I/xyz(2337): Boot Receiver - Shutdown event
05-03 16:37:28.529: I/xyz(2337): Inserting Data

Thanks!

回答1:

you can add a flag to avoid this. once you received this intent, set the flag.

if(flag)
{
     do sth.
     flag =0 ;
}
else
{
    ignore.
}