android Hander Service 广播的综合使用案例
android Hander ?Service ?廣播的綜合使用案例
原理:在主activity里啟動(dòng)一個(gè)服務(wù),這個(gè)服務(wù)通過(guò)Handler每一秒發(fā)送一條廣播,并在主activity里注冊(cè)一個(gè)廣播接受者(BroadcastReceiver),這個(gè)廣播接收者的作用在于搜到server的廣播,并是的i加1,并且把i的值設(shè)置到textview,達(dá)到美妙加1的效果
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.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
Button button1;
Button button2;
TextView textView;
publicstatic int i = 0;
publicstatic Handler handler;
Intent? intent1;
MyBroadcastReceiver mybroadcast;
@Override
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button)findViewById(R.id.start);
button2 = (Button)findViewById(R.id.stop);
textView = (TextView)findViewById(R.id.number);
button1.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
handler= new Handler(){
@Override
publicvoid handleMessage(Message msg) {
super.handleMessage(msg);
sendBroadcast(new Intent().setAction("ACTION"));
this.sendEmptyMessageDelayed(1, 1000);
}
};
Log.d("--","-------------------" + "onclick_start");
intent1 =new Intent(MainActivity.this, MyService.class);
startService(intent1);
IntentFilter intentFilter =new IntentFilter();
mybroadcast =new MyBroadcastReceiver();
intentFilter.addAction("ACTION");
MainActivity.this.registerReceiver(mybroadcast, intentFilter);
}
});
button2.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
unregisterReceiver(mybroadcast);
stopService(intent1);
if(handler !=null){
handler.removeMessages(1);
handler =null;
}
}
});
}
class MyBroadcastReceiverextends BroadcastReceiver{
@Override
publicvoid onReceive(Context context, Intent intent) {
i++;
? ? ? ? if (intent.getAction().equals("android.intent.action.DATE_CHANGED")) { ?? ? ? ? ? ?
? ? ? ? } else if (intent.getAction().equals("ACTION")) { ?? ? ? ?
? ? ? ? }?
? ? ? ? textView.setText(i + "");
}
}
}
定義一個(gè)Service
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
public class MyService extends Service{
@Override
public IBinder onBind(Intent intent) {
returnnull;
}
@Override
publicvoid onCreate() {
super.onCreate();
}
@Override
publicint onStartCommand(Intent intent, int flags,int startId) {
MainActivity.handler.sendEmptyMessageDelayed(1, 1000);
returnsuper.onStartCommand(intent, flags, startId);
}
@Override
publicvoid onDestroy() {
super.onDestroy();
if(MainActivity.handler !=null){
MainActivity.handler.removeMessages(1);
MainActivity.handler =null;
}
}
}
</activity>
? ? ? ? <service android:name="com.example.myhandlertest.MyService"></service> ? //注意AndroidManifest.xml文件中必須加上這一行
? ? </application>
運(yùn)行的效果圖:
總結(jié)
以上是生活随笔為你收集整理的android Hander Service 广播的综合使用案例的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 使用android frame动画定义
- 下一篇: BaseAdapter的一个简单案例