[英]Android. Broadcast Receiver
嘗試使用廣播接收器時遇到一些麻煩。
目標:我有三個可以在下一個架構1下工作的應用程序。首先-是廣播接收器應用程序,它將在收到消息時將一些數據寫入數據庫。 2.第二-是Android應用程序,它將發送一些意圖數據,這些意圖必須保存在數據庫中。 3.第三-是主屏幕中的小部件,該小部件還將發送一些意圖,這些意圖必須保存在數據庫中。
因此,我在Eclipse上制作了三個應用程序。 1. BroadcastReceiverExample-廣播接收器,它具有下一個文件
package com.test.receive;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class SimpleReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "service get started action", Toast.LENGTH_LONG).show();
Log.e("START","START");
}
}
和清單文件源
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:enabled="true" android:name=".receive.SimpleReceiver" android:exported="false">
<intent-filter android:priority="999">
<action android:name="com.test.SIMPLE_TEST_SERVICE"></action>
</intent-filter>
</receiver>
</application>
</manifest>
我也在Eclipse中創建了App項目(BroadcastSenderExample),並且它的文件帶有下一個發送者代碼
package com.test.sender;
import com.test.R;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class BroadcastSenderExample extends Activity {
public final static String ACTION="com.test.SIMPLE_TEST_SERVICE";
public final static String TYPE="type";
public final static int START=1;
public final static int STOP=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnStart=(Button)findViewById(R.id.btnStart);
btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent bcIntent=new Intent(ACTION);
sendBroadcast(bcIntent);
}
});
btnEnd=(Button)findViewById(R.id.btnEnd);
btnEnd.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent bcIntent=new Intent(ACTION);
sendBroadcast(bcIntent);
}
});
}
private Button btnStart=null;
private Button btnEnd=null;
}
然后,我在設備上安裝了第一個應用程序(模擬器也嘗試了),然后安裝了第二個應用程序。 然后第二個應用程序運行intent調用什么也沒有發生。
我究竟做錯了什么?
我用下一個代碼制作兩個項目
計划一個wBRReceiver
文件WBRReceiver.java
軟件包com.x.brreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class WBRReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context arg0, Intent arg1) {
Log.i("THIS IS A TEST RECEIVER","THIS IS A TEST RECEIVER");
Toast.makeText(arg0, "this is a test receiver", Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.x.brreceiver"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name="WBRReceiver">
<intent-filter>
<action android:name="com.x.START"></action>
</intent-filter>
</receiver>
</application>
</manifest>
並計划兩個wBRSender
文件WBRSenderActivity.java
package com.x.brsender;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class WBRSenderActivity extends Activity {
private String ACTION_NAME="com.x.START";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent brr=new Intent(ACTION_NAME);
//I can't use this
//brr.setClass(this, WBRReceiver.class);
//Because i just don't have this class in this case
sendBroadcast(brr);
}
}
並體現
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.x.brsender"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".WBRSenderActivity"
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>
然后,我將第一個應用程序安裝到模擬器上,然后運行第二個應用程序。 而且有效。
您看過logcat的輸出了嗎? 它很有可能會告訴您確切的問題所在。
不必過多地盯着您的代碼,看來您的清單已損壞。 在您的接收器中,您聲明android:name為“ .receive.SimpleReceiver” ...此值(以。開頭時)不只是“跟隨Android包名稱的那一部分”-盡管它的工作方式是在大多數情況下,您的Android軟件包是“ com.test”,但是包含接收者的軟件包是“ com.test.receive.SimpleReceiver”,而其Java軟件包是“ com.test.receive”。請嘗試更改android:name為“ com.test.receive.SimpleReceiver”。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.