BroadcastReceiver在Android清单中声明,应用程序强制关闭。
在测试此应用程序时,我不断遇到强制关闭错误。应用程序开放罚款。。但3-4秒后,强制关闭错误对话框出现。代码包括在内。 任何帮助都将不胜感激。 谢谢 清单BroadcastReceiver在Android清单中声明,应用程序强制关闭。,android,android-intent,broadcastreceiver,intentfilter,Android,Android Intent,Broadcastreceiver,Intentfilter,在测试此应用程序时,我不断遇到强制关闭错误。应用程序开放罚款。。但3-4秒后,强制关闭错误对话框出现。代码包括在内。 任何帮助都将不胜感激。 谢谢 清单 <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.cy.headset" android:versionCode="1" android
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cy.headset"
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=".Main" >
<intent-filter>
<action android:name="android.content.Intent.ACTION_BATTERY_CHANGED" />
</intent-filter>
</receiver>
<activity
android:name="com.cy.headset.Main"
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>
XML用户界面文件
package com.cy.headset;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class Main extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent i) {
Toast.makeText(context, "Headphone connected", Toast.LENGTH_LONG).show();
}}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Main" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_margin="10dp"
android:layout_marginTop="151dp"
android:text="@string/hello_world"
android:textAlignment="center" />
您声明您的和您的都是com.cy.headset.Main
。这是行不通的
安装应用程序后,Android会尝试启动一个名为Main
的活动,但会找到一个BroadcastReceiver
将它们分为两个不同的类,一个扩展活动和一个扩展广播接收器。
<receiver android:name=".Main" >
<intent-filter>
<action android:name="android.content.Intent.ACTION_BATTERY_CHANGED" />
</intent-filter>
</receiver>
<activity
android:name="com.cy.headset.MainActivity" <<Change it to 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>
您的BroadcastReceiver类的包是Main.java是什么
<receiver android:name=".Main" >
<intent-filter>
<action android:name="android.content.Intent.ACTION_BATTERY_CHANGED" />
</intent-filter>
</receiver>
它不应该是com.cy.headset,对吗?因为它将与您的主要活动main.java发生冲突
我认为有两种解决方案,在清单中指定接收方的包
例如:
<receiver android:name="com.package.name.Main" >
<intent-filter>
<action android:name="android.content.Intent.ACTION_BATTERY_CHANGED" />
</intent-filter>
</receiver>
或者,如果您希望您的接收者位于主活动的同一个包main.java,请重命名您的接收者
例如:
<receiver android:name=".BatteryChange" >
<intent-filter>
<action android:name="android.content.Intent.ACTION_BATTERY_CHANGED" />
</intent-filter>
</receiver>
<activity
android:name="com.cy.headset.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
另一种方法是
<receiver android:name="com.cy.headset.BatteryChange" >
<intent-filter>
<action android:name="android.content.Intent.ACTION_BATTERY_CHANGED" />
</intent-filter>
</receiver>
<activity
android:name="com.cy.headset.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
您正在为活动
和广播接收器
类使用相同的名称。将一个更改为任何其他名称,或者如果BroadcastReceiver
是另一个包,则使用包括包名在内的全名
<receiver android:name="com.cy.headset.BatteryChange" >
<intent-filter>
<action android:name="android.content.Intent.ACTION_BATTERY_CHANGED" />
</intent-filter>
</receiver>
<activity
android:name="com.cy.headset.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>