(个人经验,仅供参考,错误之处,敬请谅解)
广播
静态注册:
<receiver android:name=".MyReceiver">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE"/>
</intent-filter>
</receiver>
<receiver android:name=".R1">
<intent-filter android:priority="800">
<action android:name="com.example.myapp.NORMAL_BROADCAST"/>
<action android:name="message"/>
</intent-filter>
</receiver>
<receiver android:name=".R2">
<intent-filter android:priority="1000">
<action android:name="com.example.myapp.NORMAL_BROADCAST"/>
<action android:name="message"/>
<action android:name="musicover"/>
</intent-filter>
</receiver>
示例:
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
ConnectivityManager manager= (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info=manager.getActiveNetworkInfo();
if(info!=null&&info.isAvailable()){
Toast.makeText(context,"当前处于 联网 状态",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context,"当前处于 断网 状态",Toast.LENGTH_SHORT).show();
}
}
}
public class R2 extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if(intent.getAction().equals("message")){
Toast.makeText(context,"R2 收到了 有序广播",Toast.LENGTH_SHORT).show();
abortBroadcast();
}
else {
Toast.makeText(context, "R2 收到了 普通广播", Toast.LENGTH_SHORT).show();
}
}
}
public class MyActivity extends Activity {
private BroadcastReceiver myreceiver=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int level=intent.getIntExtra("level",0);
Toast.makeText(context,"当前剩余电量= "+level,Toast.LENGTH_SHORT).show();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
IntentFilter filter=new IntentFilter();
filter.addAction(Intent.ACTION_BATTERY_CHANGED);
registerReceiver(myreceiver, filter); //动态注册
}
@Override
protected void onPause() {
super.onPause();
unregisterReceiver(myreceiver);
}
public void sendNormalBroadcast(View v){
Intent intent=new Intent("com.example.myapp.NORMAL_BROADCAST");
sendBroadcast(intent);
}
public void sendOrderedBroadcast(View v){
Intent intent=new Intent("message");
sendOrderedBroadcast(intent,null);
}
}
服务
示例:
public class MusicService extends Service {
MediaPlayer mp;
IBinder binder=new MyBinder();
public class MyBinder extends Binder {
public MusicService getService(){
return MusicService.this;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
play();
return super.onStartCommand(intent, flags, startId);
}
public void play(){
mp=MediaPlayer.create(this,R.raw.sd1);
mp.start();
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
Intent intent=new Intent("musicover");
sendBroadcast(intent);
}
});
}
@Override
public IBinder onBind(Intent intent) {
return binder;
}
@Override
public void onDestroy() {
super.onDestroy();
if(mp.isPlaying())mp.stop();
mp.release();
}
}
public class MyActivity extends Activity {
MusicService musicService;
private ServiceConnection conn=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
musicService=((MusicService.MyBinder)service).getService();
musicService.play();
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
public void startmusicservice(View v){
//sendBroadcast();
Intent intent=new Intent(this,MusicService.class);
startService(intent);
//bindService(intent,conn,BIND_AUTO_CREATE);
}
public void stopmusicservice(View v){
Intent intent=new Intent(this,MusicService.class);
stopService(intent);
}
public void play(){
musicService.play();
}
@Override
protected void onDestroy() {
super.onDestroy();
//unbindService(conn);
}
}
[注意]:
unbindService如果出现ServiceNotRegistered错误,需要仔细分析bindService流程,就是不能出现没有bindservice而执行unbindService过程,无论是没有执行过bind还是执行了一次bind却需要执行两次unbind,均会出现该错误。