在自己定义的MyReceiver类的onReceive方法将会接收到并处理收到的intent
这个自己定义的MyReceiver类最好以内部类的形式写在使用该广播的Activity中,
这样就可以使用该Activity的UI组件了,
即可实现把后台服务进程中的数据更新在UI线程中的UI组件中
//内部类
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
Toast.makeText(getApplicationContext(), intent.getStringExtra("msg"),
Toast.LENGTH_SHORT).show();
//throw new UnsupportedOperationException("Not yet implemented");
//注意,该句是自动生成的,
一定要注释掉,否则程序会崩溃重启,
无法显示出后台服务通过广播改变前台UI的效果
}
}
完整代码
第一个是Activity
public class FinsihActivity extends AppCompatActivity {
BroadcastReceiver broadcastReceiver;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_finsih);
//第一步:注册广播,绑定广播和action
try
{
broadcastReceiver = new MyReceiver();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("com.example.space.text2.action.MSGTEXT");
registerReceiver(broadcastReceiver,intentFilter);
Log.i("mss","registerReceiver(broadcastReceiver,intentFilter);");
}
catch (ParcelFormatException e)
{
Log.i("mss","catch");
}
Button button = (Button)super.findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//第二步:开始IntentService
try
{
Intent intent = new Intent(FinsihActivity.this,TextIntentService.class);
Log.i("mss","Intent intent = new Intent(FinsihActivity.this,TextIntentService.class)");
intent.setAction("com.example.space.text2.action.MSGFINSIH");
intent.putExtra("finish","这是finishactivity发送的消息");
FinsihActivity.this.startService(intent);
Log.i("mss","FinsihActivity.this.startService(intent);");
Toast.makeText(getApplicationContext(), "startService(intent)", Toast.LENGTH_SHORT).show();
}
catch(ParcelFormatException e)
{
Log.i("mss","catch");
}
}
});
}
//内部类
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//
Toast.makeText(getApplicationContext(), intent.getStringExtra("msg"), Toast.LENGTH_SHORT).show();
// TextView textView = (TextView)FinsihActivity.super.findViewById(R.id.textView13);
// textView.setText(""+intent.getStringExtra("msg"));
//throw new UnsupportedOperationException("Not yet implemented");
}
}
}
第二个是IntentService
public class TextIntentService extends IntentService {
// TODO: Rename actions, choose action names that describe tasks that this
// IntentService can perform, e.g. ACTION_FETCH_NEW_ITEMS
private static final String ACTION_MSGTEXT = "com.example.space.text2.action.MSGTEXT";
private static final String ACTION_MSGFINSIH = "com.example.space.text2.action.MSGFINSIH";
private static final String ACTION_BAZ = "com.example.space.text2.action.BAZ";
// TODO: Rename parameters
public TextIntentService() {
super("TextIntentService");
}
// /**
// * Starts this service to perform action Foo with the given parameters. If
// * the service is already performing a task this action will be queued.
// *
// * @see IntentService
// */
// // TODO: Customize helper method
// public static void startActionFoo(Context context, String param1, String param2) {
// Intent intent = new Intent(context, TextIntentService.class);
// intent.setAction(ACTION_MSGTEXT);
// intent.putExtra(EXTRA_PARAM1, param1);
// intent.putExtra(EXTRA_PARAM2, param2);
// context.startService(intent);
// }
//
// /**
// * Starts this service to perform action Baz with the given parameters. If
// * the service is already performing a task this action will be queued.
// *
// * @see IntentService
// */
// // TODO: Customize helper method
// public static void startActionBaz(Context context, String param1, String param2) {
// Intent intent = new Intent(context, TextIntentService.class);
// intent.setAction(ACTION_BAZ);
// intent.putExtra(EXTRA_PARAM1, param1);
// intent.putExtra(EXTRA_PARAM2, param2);
// context.startService(intent);
// }
@Override
protected void onHandleIntent(Intent intent) {
Log.i("mss","onHandleIntent");
if (intent != null) {
final String action = intent.getAction();
if (ACTION_MSGFINSIH.equals(action)) {
Log.i("mss","ACTION_MSGFINSIH.equals(action)");
String param1 = intent.getStringExtra("finish");
Log.i("mss","intent.getStringExtra(\"finish\") = "+param1);
Intent intent1 = new Intent(ACTION_MSGTEXT);
Log.i("mss","Intent intent1 = new Intent(ACTION_MSGTEXT);");
Log.i("mss",ACTION_MSGTEXT);
intent1.putExtra("msg",param1+",IntentService收到并且广播了出去");
Log.i("mss","intent1.putExtra(\"msg\",param1+\",IntentService收到并且广播了出去\");");
sendBroadcast(intent1);
Log.i("mss","super.sendBroadcast(intent1);");
}
}
}
// /**
// * Handle action Foo in the provided background thread with the provided
// * parameters.
// */
// private void handleActionFoo(String param1, String param2) {
// // TODO: Handle action Foo
// throw new UnsupportedOperationException("Not yet implemented");
// }
// private void handleActionFinish(String param1, String param2) {
// // TODO: Handle action Foo
// throw new UnsupportedOperationException("Not yet implemented");
// }
// /**
// * Handle action Baz in the provided background thread with the provided
// * parameters.
// */
// private void handleActionBaz(String param1, String param2) {
// // TODO: Handle action Baz
// throw new UnsupportedOperationException("Not yet implemented");
// }
}