こんにちは、放送受信機の登録に問題があります。理由がわかりません。これは私のコードです
主な活動
public class Service extends Activity {
private MyServiceReceiver myReceiver;
TextView txtManual;
TextView txtAuto;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_service);
IntentFilter filter;
filter = new IntentFilter(MyService.NEW_DATA);
myReceiver = new MyServiceReceiver();
registerReceiver(myReceiver, filter);
txtAuto = (TextView) findViewById(R.id.txtAuto);
txtManual = (TextView) findViewById(R.id.txtManual);
Button cmdStart = (Button) findViewById(R.id.cmdStart);
cmdStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
startService(new Intent(Service.this, MyService.class));
}
});
Button cmdRefresh = (Button) findViewById(R.id.cmdRefresh);
}
public class MyServiceReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
txtAuto.setText(intent.getStringExtra("data"));
if(intent.getStringExtra("data")== null){
txtManual.setText("Null");
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
unregisterReceiver(myReceiver);
super.onPause();
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
unregisterReceiver(myReceiver);
super.onStop();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
IntentFilter filter;
filter = new IntentFilter(MyService.NEW_DATA);
myReceiver = new MyServiceReceiver();
registerReceiver(myReceiver, filter);
super.onResume();
}}
これは私のマニフェストです:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.t8ej1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="17"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<service android:name="MyService"/>
<activity
android:name="com.example.t8ej1.Service"
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>
And this is my logcat
07-08 01:44:26.328: E/AndroidRuntime(6315): FATAL EXCEPTION: main
07-08 01:44:26.328: E/AndroidRuntime(6315): java.lang.RuntimeException: Unable to stop activity
{com.example.t8ej1/com.example.t8ej1.Service}: java.lang.IllegalArgumentException: Receiver not registered: com.example.t8ej1.Service$MyServiceReceiver@40d10a30
07-08 01:44:26.328: E/AndroidRuntime(6315): at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3415)
07-08 01:44:26.328: E/AndroidRuntime(6315): at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3469)
07-08 01:44:26.328: E/AndroidRuntime(6315): at android.app.ActivityThread.access$1200(ActivityThread.java:141)
07-08 01:44:26.328: E/AndroidRuntime(6315): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1287)
07-08 01:44:26.328: E/AndroidRuntime(6315): at android.os.Handler.dispatchMessage(Handler.java:99)
07-08 01:44:26.328: E/AndroidRuntime(6315): at android.os.Looper.loop(Looper.java:137)
07-08 01:44:26.328: E/AndroidRuntime(6315): at android.app.ActivityThread.main(ActivityThread.java:5041)
07-08 01:44:26.328: E/AndroidRuntime(6315): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 01:44:26.328: E/AndroidRuntime(6315): at java.lang.reflect.Method.invoke(Method.java:511)
07-08 01:44:26.328: E/AndroidRuntime(6315): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-08 01:44:26.328: E/AndroidRuntime(6315): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-08 01:44:26.328: E/AndroidRuntime(6315): dalvik.system.NativeStart.main(ネイティブ メソッド) 07-08 01:44:26.328: E/AndroidRuntime(6315): 原因: java.lang .IllegalArgumentException: 受信者が登録されていません: com.example.t8ej1.Service$MyServiceReceiver@40d10a30 07-08 01:44:26.328: E/AndroidRuntime(6315): android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:657) 07 -08 01:44:26.328: E/AndroidRuntime(6315): android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1339) 07-08 01:44:26.328: E/AndroidRuntime(6315): android.content で.ContextWrapper.unregisterReceiver(ContextWrapper.java:445) 07-08 01:44:26.328: E/AndroidRuntime(6315): com.example.t8ej1.Service.onStop(Service.java:85) 07-08 01:44 :26.328: E/AndroidRuntime(6315): android.app.Instrumentation で。callActivityOnStop(Instrumentation.java:1205) 07-08 01:44:26.328: E/AndroidRuntime(6315): android.app.Activity.performStop(Activity.java:5246) 07-08 01:44:26.328: E/ AndroidRuntime (6315): android.app.ActivityThread.performDestroyActivity (ActivityThread.java:3410) で
OnCreate
andメソッドに放送受信機を登録し、 andメソッドOnResume
の登録を解除しましたが、アプリケーションを終了するとエラーが発生しました!!OnPause
OnStop