BroadcastReceiver的ACTION_SHUTDOWN行为

时间:2013-05-03 11:23:47

标签: android broadcastreceiver

我有BroadcastReceiver听取ACTION_SHUTDOWN和其他一些行动。

我的问题是,当我关闭我的Android设备(2.3.6)时,BroadcastReceiver正在捕捉ACTION_SHUTDOWN两次。问题仅与ACTION_SHUTDOWN有关,而与其他操作无关。

当我在模拟器上运行相同的代码时,它工作正常 伙计们请帮帮我。这是我的代码:

我的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>

这是Logcat条目

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

谢谢!

1 个答案:

答案 0 :(得分:0)

你可以添加一个标志来避免这种情况。 一旦收到此意图,请设置标志。

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