我正在尝试将广播发送到另一个包中的服务。我的活动通过bindService连接到服务。我注意到,当我发送广播时,服务永远不会收到,除非我在onServiceConnected方法中发送广播,它会被收到并且一切正常!是的,如果我在我的活动的任何其他部分发送广播,这个广播永远不会收到?更具体地说,我的服务注册了一个本地广播,因此它在onCreate和onDestroy方法中进行注册和取消注册:
这是Application_A的代码,它将广播发送到服务。在onStart方法中,它启动远程服务并绑定到它,在onConnected方法中它正确执行广播。另一方面,当我点击按钮时,它没有发送广播,但它确实显示了吐司。
package com.example.app_a;
import android.app.Activity;
import android.app.PendingIntent;
import android.app.PendingIntent.CanceledException;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivityA extends Activity implements OnClickListener
{
private ImageButton button;
private EditText inputEditText;
private boolean mIsBound = false;
private Intent in;
private static final String MSG_CHANGE_TEXT_COLOR = "CHANGE_TEXT_COLOR";
private static final String BroadCast_Filter = "com.example.app_a";
private class AppReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
}
}
private ServiceConnection mConnection = new ServiceConnection()
{
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
mIsBound = true;
Toast.makeText(getApplicationContext(), "CONNECTED TO SERVICE!", Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setAction("com.example.talker_service.Communicate");
/*
intent.putExtra("REQUEST", "REGISTER APP");
intent.putExtra("FILTER", BroadCast_Filter);
intent.putExtra("NAME", getResources().getString(R.string.app_name));
Toast.makeText(getApplicationContext(), "APP NAME: "+getResources().getString(R.string.app_name), Toast.LENGTH_SHORT).show();
String[] components = {"NUMBER_SENT",MSG_CHANGE_TEXT_COLOR,"CHANGE_TEXTVIEW_SIZE"};
intent.putExtra("COMPONENTS", components);
sendBroadcast(intent);
*/
intent.putExtra("REQUEST","SEND" );
intent.putExtra("FILTER", BroadCast_Filter);
intent.putExtra("COMPONENT", MSG_CHANGE_TEXT_COLOR );
intent.putExtra("DATA", "#000099");
MainActivityA.this.getApplicationContext().sendBroadcast(intent);
}
@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
mIsBound = false;
Toast.makeText(getApplicationContext(), "CONNECTION LOST FROM APP_A", Toast.LENGTH_SHORT).show();
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (ImageButton) this.findViewById(R.id.invioImageButton);
inputEditText = (EditText) this.findViewById(R.id.inputEditTetxt);
button.setOnClickListener(this);
}
@Override
protected void onStart()
{
// TODO Auto-generated method stub
super.onStart();
//Bind to service
in = new Intent();
in.setClassName("com.example.app_talker_service", "com.example.app_talker_service.Talker_Service");
//this.startService(in);
bindService(in, mConnection,Context.BIND_AUTO_CREATE);
//startService(new Intent("com.example.app_b.exampleservice"));
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
//this.stopService(in);
/*if(mIsBound)
{
getApplicationContext().unbindService(mConnection);
mIsBound = false;
}*/
}
@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
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.invioImageButton:
Toast.makeText(getApplicationContext(), "BUTTON PRESSED", Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
in.setClassName("com.example.app_talker_service", "com.example.talker_service.Communicate");
intent.setType("text/plain");
intent.putExtra("REQUEST","SEND" );
intent.putExtra("FILTER", BroadCast_Filter);
intent.putExtra("COMPONENT", MSG_CHANGE_TEXT_COLOR );
intent.putExtra("DATA", "#5C5CE6");
MainActivityA.this.getApplicationContext().sendBroadcast(intent);
break;
}
}
}
该服务位于另一个包中并完美地发送任何广播,但不知何故,如果不在onserviceconnected方法中发送,它不会从活动中发现它们。我无法发布该课程的所有代码很大,但我会向您展示主要部分。请注意,该服务只是一个经过修改的服务,因此它不能与ServiceIntent一起使用,但它有一个处理程序,以便与Message类型数据进行通信。但是我试图出于个人原因使用广播:
package com.example.app_talker_service;
public class Talker_Service extends Service
{
private RegistryDB registry;
private TalkerService_Receiver receiver;
private IntentFilter filter;
public final Messenger mMessenger = new Messenger(new IncomingHandler());
...
...
...
public class TalkerService_Receiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
...
...
...
}
@Override
public void onCreate()
{
// TODO Auto-generated method stub
registry = new RegistryDB(this.getApplicationContext());
receiver = new TalkerService_Receiver();
filter = new IntentFilter("com.example.talker_service.Communicate");
this.registerReceiver(receiver,filter );
Toast.makeText(getApplicationContext(), " RECEIVER REGISTERED",
Toast.LENGTH_SHORT).show();
super.onCreate();
}
@Override
public void onDestroy()
{
// TODO Auto-generated method stub
this.unregisterReceiver(receiver);
Toast.makeText(getApplicationContext(), " RECEIVER UNREGISTERED", Toast.LENGTH_SHORT).show();
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent)
{
this.registerReceiver(receiver,filter );
return mMessenger.getBinder();
}
任何人都可以向我解释为什么当我不使用onconnected方法发送广播时,广播似乎无法正常工作?是否可以从活动中发送广播服务?