我在尝试使用广播接收器时遇到了一些麻烦。
目标:我有三个应用程序将在下一个模式 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;
}
然后我在设备上安装第一个应用程序(也尝试模拟器),然后安装第二个应用程序。然后第二个应用程序运行意图调用什么也没发生。
我究竟做错了什么?
我用下一个代码做了两个项目
项目一 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>
然后我将第一个应用程序安装到模拟器上,然后运行第二个应用程序。它有效。