Android学习笔记之Broadcast Receiver
可程序间通信
注册通信,注销通信,发送消息
package com.jiahemeikang.helloandroid; import com.jiahemikang.service.EchoService; import com.jiahemikang.service.EchoService.EchoServiceBingder; import com.jikexuyua.broadcastreceiver.MyBC; import android.os.Bundle; import android.os.IBinder; import android.app.Activity; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.ServiceConnection; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity implements OnClickListener,ServiceConnection{ private TextView tvResult ; private Intent serviceIntent; private Button btnStarAty1; private Button btnStarService; private Button btnStopService; private Button btnBingService; private Button btnUnBingService; private Button btnGetNum; private Button btnbroadcast; private Button regbroadcastButton; private Button btnunregbroadcastButton; public EchoService echoService = null; @Override protected void onCreate(Bundle savedInstanceState) { serviceIntent = new Intent(this,EchoService.class); super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvResult = (TextView)findViewById(R.id.tvResult); btnStarAty1= (Button)findViewById(R.id.btnStartAty1); btnStarService= (Button)findViewById(R.id.btnStartService); btnStopService= (Button)findViewById(R.id.btnStopService); btnStarService.setOnClickListener(this); btnStopService.setOnClickListener(this); btnBingService= (Button)findViewById(R.id.bingdingservice); btnUnBingService= (Button)findViewById(R.id.unbingdingservice); btnBingService.setOnClickListener(this); btnUnBingService.setOnClickListener(this); btnGetNum= (Button)findViewById(R.id.btnGetNum); btnGetNum.setOnClickListener(this); btnbroadcast= (Button)findViewById(R.id.btnbroadcast); btnbroadcast.setOnClickListener(this); regbroadcastButton= (Button)findViewById(R.id.btnregbroadcast); regbroadcastButton.setOnClickListener(this); btnunregbroadcastButton= (Button)findViewById(R.id.btnunregbroadcast); btnunregbroadcastButton.setOnClickListener(this); btnStarAty1.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v){ Intent i = new Intent(MainActivity.this,Aty1.class); i.putExtra("txt","Hello aty1"); startActivity(i); startActivityForResult(i,0); } }); } @Override protected void onActivityResult(int a,int b ,Intent i){ if (i!=null) { String result = i.getStringExtra("result"); tvResult.setText(result); } } @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; } private final MyBC myBC = new MyBC(); @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btnStartService: startService(serviceIntent); break; case R.id.btnStopService: stopService(serviceIntent); break; case R.id.bingdingservice: bindService(serviceIntent,this,Context.BIND_AUTO_CREATE); break; case R.id.unbingdingservice: unbindService(this); echoService =null; break; case R.id.btnGetNum: if (echoService!=null) { System.out.print("当前服务的 数字为"+echoService.getCountNum()); } break; case R.id.btnbroadcast: Intent i = new Intent(MyBC.ACTION); i.putExtra("txt", "消息"); sendBroadcast(i); break; case R.id.btnregbroadcast: registerReceiver(myBC, new IntentFilter(MyBC.ACTION)); break; case R.id.btnunregbroadcast: unregisterReceiver(myBC); break; default: break; } } @Override public void onServiceConnected(ComponentName arg0, IBinder binder) { System.out.println("onServiceConnected"); echoService = ((EchoService.EchoServiceBingder)binder).getService(); } @Override public void onServiceDisconnected(ComponentName arg0) { // TODO Auto-generated method stub } //onStart }
broadcast继承BroadcastReceiver
package com.jikexuyua.broadcastreceiver; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; public class MyBC extends BroadcastReceiver { public static final String ACTION = "com.jikexuyua.broadcastreceiver.intent.action.MyBC"; public MyBC() { // TODO Auto-generated constructor stub } @Override public void onReceive(Context arg0, Intent arg1) { // TODO Auto-generated method stub System.out.println("OnReceiver data"+arg1.getStringExtra("txt")); } }