操作:
(1)、广播发送(开启service):在application中配置一个全局定时器 规定多长时间去服务器端获取一次数据 (写一个service 这个service就是去服务端请求数据的 但是这个service的启动时根据广播通知)
(2)、广播发送(修改ui):获取到数据后在本地存储 使用reciver广播通知activity活动页有更新了 在activity中进行拦截广播 并展示数据
具体的实现:
(1)、Myapplice的定时器:定时发送广播–启动service
public class MyApplication extends Application {
private static final String BANGBANG_SETTING = "zw";// 要改,屏蔽不同版本等信息?
private static MyApplication application=null;
public static MyApplication getInstance(){
return application;
}
public static boolean isGetMsg = false;//是否需要用到轮询 在登陆成功就开始使用 或者 开始http聊天时使用
private AlarmManager am;//全局定时器
@Override
public void onCreate() {
super.onCreate();
application = this;
initPoll();
}
//轮询
private void initPoll(){
Intent intent = new Intent(this, MyReciver.class);
intent.setAction(Constant.ReceiverAction.MSG_POLL);
//送达
PendingIntent sender = PendingIntent.getBroadcast(this,0,intent,0);
long firstime = SystemClock.elapsedRealtime();//最初时间
am = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
//10秒一个周期 不停的发送广播
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,firstime,10*1000,sender);
}
(2)、service的开启
public class MyReciver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//逛街广播通知来启动service
String action = intent.getAction();
if(Constant.ReceiverAction.MSG_POLL.equals(action)&& MyApplication.isGetMsg){
Intent i = new Intent();
i.setClass(context, MyService.class);
context.startService(i);
}
}
}
(3)、service的开启另个广播:更新UI
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
//初始化的操作
}
@Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
//网络是否可用
if(new NetWorkUtils(this).CheckNetwork()){
//
Log.i("yigeren","来了一次");
MyApplication.getInstance().setSharedPreferencesValue("yig","来了一次");
//在发送一个广播,更新UI
sendBroadcast(new Intent(Constant.ReceiverAction.TONGZHI_ACTION));
}
}
}
(4)、界面的操作:标识控制、
public class MyActivity extends Activity implements View.OnClickListener {
private Button btn_start,btn_stop;//启动和结束
private Myreciver2 myreciver2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn_start = (Button) findViewById(R.id.btn_start);
btn_stop = (Button) findViewById(R.id.btn_stop);
btn_start.setOnClickListener(this);
btn_stop.setOnClickListener(this);
//注册广播
myreciver2 = new Myreciver2();
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Constant.ReceiverAction.TONGZHI_ACTION);
registerReceiver(myreciver2,intentFilter);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btn_start:
MyApplication.isGetMsg = true;
break;
case R.id.btn_stop:
MyApplication.isGetMsg = false;
break;
}
}
@Override
protected void onDestroy() {
super.onDestroy();
//注销广播
unregisterReceiver(myreciver2);
}
class Myreciver2 extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
String s = MyApplication.getInstance().getSharedPreferencesValue("yig");
Toast.makeText(context,s,Toast.LENGTH_SHORT).show();
}
}
}
(5)、xml配置:
<application android:label="@string/app_name"
android:name=".sys.MyApplication"
android:icon="@drawable/ic_launcher">
<activity
android:name="MyActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver android:name=".reciver.MyReciver" android:exported="true"/>
<service android:name=".service.MyService" android:enabled="true"/>
</application>