Android服务内部的BroadcastReceiver:问题
Android BroadcastReceiver inside service: issues
我目前正在尝试在 Android 中创建一个 Activity,它具有与服务内部启动的 BroadcastReceiver
进行通信的能力,但我做不好.我真的不知道问题出在哪里,因为(我认为)我已经执行了所有必要的步骤。
此外,我还有其他活动可以毫无问题地与此 BroadcastReceiver
通信。我在遇到问题时使用的代码如下:
在文件ActivityList.java
中注册动作名称(另一个activity):
public static final String ACTION1 = "com.test.ActionOne";
public static final String ACTION2 = "com.test.ActionTwo";
在扩展 Service
:
的文件 GestTree.java
中使用 IntentFilter
注册操作
里面 onCreate()
:
IntentFilter filter;
filter = new IntentFilter();
filter.addAction(ActivityList.ACTION1);
filter.addAction(ActivityList.ACTION2);
rec = new Receptor(); // This is a class which extends BroadcastReceiver
registerReceiver(receptor, filter);
在 GestTree.java
的 private class Receptor
的函数 onReceive()
中扩展了 BroadcastReceiver
:
public final void onReceive(final Context context, final Intent intent) {
String action = intent.getAction();
if (action.equals(ActivityList.ACTION1)) {
Log.d(tag, "Test Passed!");
}
}
服务的定义和Activity State3Activity
(我要与服务通信的那个)在AndroidManifest.xml
:
<activity
android:name="State3Activity"
android:label="@string/app_name" >
</activity>
<service
android:name="GestTree"
android:enabled="true"
android:label="@string/app_name" >
</service>
里面的代码State3Activity.java
:
public class State3Activity extends Activity {
Button mButton;
EditText editText_Name;
EditText editText_Desc;
private final String tag = this.getClass().getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.state3_layout);
mButton = (Button)findViewById(R.id.button_myButton);
editText_Nombre = (EditText)findViewById(R.id.editText_Name);
editText_Descripcion = (EditText)findViewById(R.id.editText_Desc);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Intent intent;
intent = new Intent();
intent.setAction(ActivityList.ACTION1);
// I have tried with all this combination of lines
// but none of them works
//intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
//intent.setClass(State3Activity.this, GestTree.class);
//intent.addCategory(Intent.CATEGORY_DEFAULT);
sendBroadcast(intent);
}
});
}
问题就出在这里。当我按下按钮时,意图永远不会进入 class 的 onReceive()
功能。我要离开什么?
你确定你的服务启动了吗?如果你还没有启动服务,onCreate()
的方法将不会被执行,接收者也不会被注册。
我目前正在尝试在 Android 中创建一个 Activity,它具有与服务内部启动的 BroadcastReceiver
进行通信的能力,但我做不好.我真的不知道问题出在哪里,因为(我认为)我已经执行了所有必要的步骤。
此外,我还有其他活动可以毫无问题地与此 BroadcastReceiver
通信。我在遇到问题时使用的代码如下:
在文件ActivityList.java
中注册动作名称(另一个activity):
public static final String ACTION1 = "com.test.ActionOne";
public static final String ACTION2 = "com.test.ActionTwo";
在扩展 Service
:
GestTree.java
中使用 IntentFilter
注册操作
里面 onCreate()
:
IntentFilter filter;
filter = new IntentFilter();
filter.addAction(ActivityList.ACTION1);
filter.addAction(ActivityList.ACTION2);
rec = new Receptor(); // This is a class which extends BroadcastReceiver
registerReceiver(receptor, filter);
在 GestTree.java
的 private class Receptor
的函数 onReceive()
中扩展了 BroadcastReceiver
:
public final void onReceive(final Context context, final Intent intent) {
String action = intent.getAction();
if (action.equals(ActivityList.ACTION1)) {
Log.d(tag, "Test Passed!");
}
}
服务的定义和Activity State3Activity
(我要与服务通信的那个)在AndroidManifest.xml
:
<activity
android:name="State3Activity"
android:label="@string/app_name" >
</activity>
<service
android:name="GestTree"
android:enabled="true"
android:label="@string/app_name" >
</service>
里面的代码State3Activity.java
:
public class State3Activity extends Activity {
Button mButton;
EditText editText_Name;
EditText editText_Desc;
private final String tag = this.getClass().getSimpleName();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.state3_layout);
mButton = (Button)findViewById(R.id.button_myButton);
editText_Nombre = (EditText)findViewById(R.id.editText_Name);
editText_Descripcion = (EditText)findViewById(R.id.editText_Desc);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Intent intent;
intent = new Intent();
intent.setAction(ActivityList.ACTION1);
// I have tried with all this combination of lines
// but none of them works
//intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
//intent.setClass(State3Activity.this, GestTree.class);
//intent.addCategory(Intent.CATEGORY_DEFAULT);
sendBroadcast(intent);
}
});
}
问题就出在这里。当我按下按钮时,意图永远不会进入 class 的 onReceive()
功能。我要离开什么?
你确定你的服务启动了吗?如果你还没有启动服务,onCreate()
的方法将不会被执行,接收者也不会被注册。