看看服务。如果您在应用程序中使用服务,则很多情况下不必进行明确编码。
编辑
在回应评论,这里就是我如何使用广播接收器
public class SomeActivity extends Activity {
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// Example of pulling a string out of the Intent's "extras"
String msg = intent.getStringExtra("BroadcastString");
// ...more stuff
}
};
@Override
public void onResume()
{
super.onResume();
registerReceiver(receiver, new IntentFilter("SomeStringKeyForMyBroadcast"));
// ... other stuff
}
@Override
public void onPause()
{
super.onPause();
unregisterReceiver(receiver);
// ... other stuff
}
,并在我的服务做到从服务到活动通信...
public class SomeService extends Service {
Intent broadcastIntent = new Intent("SomeStringKeyForMyBroadcast");
private void someWorkerMethodInMyService()
{
// ... other stuff
broadcastIntent.putExtra("BroadcastString", "Some Data");
sendBroadcast(broadcastIntent);
// ... other stuff
}
类似的东西...