Android中,service和activity是2大组件,如何在后台运行service,并且控制前台activity的显示呢,这里用到了广播。
这里就不废话了,先贴activity代码:
<span style="font-size:12px;">package com.example.broadcasewithservice;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.tv);
Intent intent = new Intent(MainActivity.this,MyService.class);
this.startService(intent);
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
registerReceiver();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
unregisterReceiver(mReceiver);
}
//注册广播
public void registerReceiver() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction("<span style="color:#ff0000;">com.zhanghao.msg</span>");
registerReceiver(mReceiver, intentFilter);
}
//接收广播
private BroadcastReceiver mReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if(action.equals("com.zhanghao.msg")){
String stringExtra = intent.getStringExtra("msg");
tv.setText(stringExtra);
}
}
};
}
</span>
然后贴上service的代码:
<span style="font-size:12px;">package com.example.broadcasewithservice;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
Log.i("MyService", "onBind");
sendMsg("onBind");
return null;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
Log.i("MyService", "onCreate");
sendMsg("onCreate");
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.i("MyService", "onDestroy");
sendMsg("onDestroy");
}
@Override
public void onRebind(Intent intent) {
// TODO Auto-generated method stub
super.onRebind(intent);
Log.i("MyService", "onRebind");
sendMsg("onRebind");
}
@Override
@Deprecated
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
Log.i("MyService", "onStart");
sendMsg("onStart");
}
@Override
public boolean onUnbind(Intent intent) {
// TODO Auto-generated method stub
Log.i("MyService", "onUnbind");
sendMsg("onUnbind");
return super.onUnbind(intent);
}
</span><span style="font-size:12px;"><span style="color:#ff0000;">// 发送广播
private void sendMsg(String msg) {
Intent intent = new Intent("com.zhanghao.msg");
intent.putExtra("msg", msg);
this.sendBroadcast(intent);
}
</span>}
</span>
这里的广播注册是在代码中注册的。也可以在AndroidManifast.xml注册,这里就不废话了。service需要在AndroidManifast.xml注册,代码:
<span style="font-size:12px;"><application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
</span><span style="font-size:12px;"><span style="color:#ff0000;"><service android:name=".MyService"></service>
</span>
<activity
android:name="com.example.broadcasewithservice.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application></span>
这是一个简单的demo,代码书写力求简单,所以有很多方面没有考虑到。