Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android广播接收器可以在模拟器上工作,但不能在设备上工作_Android_Android Intent_Android Emulator_Android Broadcast - Fatal编程技术网

Android广播接收器可以在模拟器上工作,但不能在设备上工作

Android广播接收器可以在模拟器上工作,但不能在设备上工作,android,android-intent,android-emulator,android-broadcast,Android,Android Intent,Android Emulator,Android Broadcast,下面是设置。。我有两个AppReceiver.apk和AppClient.apk应用程序。AppReceiver项目只有一个接收器,没有其他活动,AppClient只发送广播消息。这在我的mac上使用Android 4.0 api级别14的仿真器中有效,但在windows或实际设备上不起作用 AppReceiver.apk的代码 *MyReceiver.java* 清单文件 <manifest xmlns:android="http://schemas.android.com/apk/re

下面是设置。。我有两个AppReceiver.apk和AppClient.apk应用程序。AppReceiver项目只有一个接收器,没有其他活动,AppClient只发送广播消息。这在我的mac上使用Android 4.0 api级别14的仿真器中有效,但在windows或实际设备上不起作用

AppReceiver.apk的代码 *MyReceiver.java*

清单文件

<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>
<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>
清单文件

<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>
<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”,“Get message”);在我的日志中,这意味着它正在按预期工作。我可以点击多次,效果如预期


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

所以我确认在安装后必须至少启动AppReceiver.apk一次,这是安卓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>
android:allowBackup=“true” android:icon=“@drawable/ic_启动器” android:label=“@string/app\u name” android:theme=“@style/AppTheme”>
在发送广播之前,您是否启动了接收器“应用程序”?我认为您的receiver应用程序中至少需要有一个活动,因为最新的Android版本要求该应用程序至少启动一次,以使其静态注册的receiver处于活动状态。我如何启动该应用程序,因为我不希望在AppReceiver.apk中有任何活动,因为启动活动将启动gui,并且该接收器是用于我做不到的事情。正如我所说的,它可以在我的mac模拟器和安卓4.0上运行。但不启动它可能是一个很好的问题,但我如何解决它与我得到的限制?尝试设置您的目标SDk版本在接收器应用程序为10或以下。选中此项: