【问题标题】:Android Broadcast receiver works on emulator but not on deviceAndroid广播接收器适用于模拟器但不适用于设备
【发布时间】:2013-09-17 05:24:43
【问题描述】:

所以这里是设置.. 我有两个应用 AppReceiver.apk 和 AppClient.apk。 AppReceiver 项目只有一个接收器,没有其他活动,AppClient 只发送广播消息。这适用于我的 Mac 上 Android 4.0 api level 14 的模拟器,但不适用于 Windows 或实际设备。

AppReceiver.apk 的代码 *MyReceiver.java*

  public class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            Log.d("MyReceiver", "Got Message");
        }

    }

清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.appreceiver"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <receiver android:name="MyReceiver">
            <intent-filter>
                <action android:name="com.appreceiver.CUSTOM_INTENT"></action>
            </intent-filter>
        </receiver>
    </application>

</manifest>

AppClient 代码 MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button sendButton = (Button)findViewById(R.id.sendbroadcastButton);
    sendButton.setOnClickListener(new View.OnClickListener(){
    public void onClick(View v) 
    {
        Intent intent = new Intent();
        intent.setAction("com.appreceiver.CUSTOM_INTENT");
        sendBroadcast(intent); 
         }  
});     
}

清单文件

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.appclient"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.appclient.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

所以当我在 Mac 中运行的模拟器中单击 sendbroadcast 按钮 时会发生什么,然后我看到日志消息 Log.d("MyReceiver", "Got Message") ;在我的 logcat 中,这意味着它按预期工作。我可以多次点击,它按预期工作。

但是,当我将它安装在设备上时,我在 logcat 中看不到消息(我确保我正在从设备读取 logcat)另外我还有另一台 Windows 笔记本电脑,在那个笔记本电脑上它不会显示此消息模拟器。所以我不确定发生了什么?有什么指点吗?

【问题讨论】:

  • 您在发送广播之前是否启动过一次接收器“应用程序”?我认为您的接收器应用程序中至少需要有一个 Activity,因为最新的 Android 版本要求该应用程序至少启动一次才能使其静态注册的接收器处于活动状态。
  • 如何启动应用程序,因为我不想在 AppReceiver.apk 中进行任何活动,因为启动活动会调出 gui,而接收器适用于我无法做到的事情。同样正如我所说,它可以在我的带有 android 4.0 的 mac 上的模拟器中运行。但是不启动它很可能是问题,但是我该如何解决我遇到的限制呢?
  • 尝试将 Receiver 应用中的目标 SDK 版本设置为 10 或更低版本。检查这个:commonsware.com/blog/2011/07/13/…

标签: android android-intent android-emulator android-broadcast


【解决方案1】:

所以我确认我必须在安装后至少启动一次 AppReceiver.apk,这是 Android 3.1 之后的新要求或安全事项,因为他们希望用户显式启动应用程序以使用其广播接收器。

由于我不想要任何 GUI 的东西,我所做的只是编写一个不调用任何视图的活动并将主题定义为 android:theme="@android:style/Theme.NoDisplay" 在清单中,因此用户在安装后不会注意到应用程序已经启动并且接收器也可用。感谢@Nitin Sethi 为我指明了正确的方向。

public class MyActivity extends Activity 
{
    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
       super.onCreate(savedInstanceState);  
    }   
}

这是 AppReceiver.apk 的新清单的外观

  android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
         <activity
            android:name="com.appreceiver.MyActivity" android:theme="@android:style/Theme.NoDisplay">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <receiver android:name="com.appreceiver.MyReceiver">
            <intent-filter>
                <action android:name="com.appreceiver.CUSTOM_INTENT"></action>           
            </intent-filter>
        </receiver>
    </application>

【讨论】:

    猜你喜欢
    • 2012-11-01
    • 2011-03-08
    • 2015-02-13
    • 2011-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多