【问题标题】:BroadcastReceiver's behaviour for ACTION_SHUTDOWN广播接收器对 ACTION_SHUTDOWN 的行为
【发布时间】:2013-05-03 20:07:40
【问题描述】:

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

谢谢!

【问题讨论】:

  • 不知道问题是什么,但作为一种解决方法,您可以在第一次关闭后将某些内容存储在 sharedprefs 中,并在第二次运行时检查它是否存在,然后您会收到启动完成删除它....

标签: android broadcastreceiver


【解决方案1】:

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

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

【讨论】:

  • 我通过使用标志来避免这种情况,但我想知道为什么会这样?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多