本文介绍了广播接收器的行为ACTION_SHUTDOWN的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

广播接收器来监听 ACTION_SHUTDOWN 和其他一些行动。

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

我的问题是,当我关闭我的Andr​​oid设备(2.3.6)广播接收器正在迎头赶上 ACTION_SHUTDOWN 两次。问题只与 ACTION_SHUTDOWN ,而不是与其他动作。

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.

当我运行模拟器同一code,它工作正常。结果
伙计们,请帮助我。这里是我的code:

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

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

的Andr​​oidManifest.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

谢谢!

推荐答案

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

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

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

这篇关于广播接收器的行为ACTION_SHUTDOWN的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-08 14:50