网上看到的,实际操作了下,例子代码如下。加深对BroadcastReceiver与Service理解
package org.yihu;
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.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;public class UIDemoActivity extends Activity {
public static final int CMD_STOP_SERVICE = 0;
Button btnStart;//开始服务Button对象应用
Button btnStop;//停止服务Button对象应用
TextView tv;//TextView对象应用
DataReceiver dataReceiver;//BroadcastReceiver对象
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//设置显示的屏幕
btnStart = (Button)findViewById(R.id.btnStart);
btnStop = (Button)findViewById(R.id.btnStop);
tv = (TextView)findViewById(R.id.tv);
btnStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Intent myIntent = new Intent(Sample_3_6.this, MyService.class);
// startService(myIntent);//发送Intent启动Service
Intent intent = new Intent();
intent.setAction("org.yihu.UIService");
startService(intent);
}
});
btnStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent myIntent = new Intent();//创建Intent对象
myIntent.setAction("org.yihu.cmdReceiver");
myIntent.putExtra("cmd", CMD_STOP_SERVICE);
sendBroadcast(myIntent);//发送广播
}
});
}
private class DataReceiver extends BroadcastReceiver{//继承自BroadcastReceiver的子类
@Override
public void onReceive(Context context, Intent intent) {
double data = intent.getDoubleExtra("data", 0);
tv.setText("Service的数据为:"+data);
}
}
@Override
protected void onStart() {
dataReceiver = new DataReceiver();
IntentFilter filter = new IntentFilter();//创建IntentFilter对象
filter.addAction("org.yihu.Sample_3_6");
registerReceiver(dataReceiver, filter);//注册Broadcast Receiver
super.onStart();
}
@Override
protected void onStop() {
unregisterReceiver(dataReceiver);//取消注册Broadcast Receiver
super.onStop();
}
package org.yihu;
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.util.Log;public class MyService extends Service{
CommandReceiver cmdReceiver;
boolean flag;
@Override
public void onCreate() {
Log.v("Myservice", "onCreate()...");
flag = true;
cmdReceiver = new CommandReceiver();
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
Log.v("Myservice", "IBinder()...");
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.v("Myservice", "onStartCommand()...");
IntentFilter filter = new IntentFilter();
filter.addAction("org.yihu.cmdReceiver");
registerReceiver(cmdReceiver, filter);//注册Broadcast Receiver
doJob();//调用方法启动线程
return super.onStartCommand(intent, flags, startId);
}
//方法:
public void doJob(){
new Thread(){
public void run(){
while(flag){
try{//睡眠一段时间
Thread.sleep(1000);
}
catch(Exception e){
e.printStackTrace();
}
Intent intent = new Intent();//创建Intent对象
intent.setAction("org.yihu.Sample_3_6");
intent.putExtra("data", Math.random());
sendBroadcast(intent);//发送广播
}
}
}.start();
}
private class CommandReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
int cmd = intent.getIntExtra("cmd", -1);
if(cmd == UIDemoActivity.CMD_STOP_SERVICE){//如果发来的消息是停止服务
flag = false;//停止线程
stopSelf();//停止服务
}
}
}
@Override
public void onDestroy() {//重写onDestroy方法
this.unregisterReceiver(cmdReceiver);//取消注册的CommandReceiver
super.onDestroy();
} }
mail.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/btnStart"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="start service"
/>
<Button
android:id="@+id/btnStop"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="stop service"
/>
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
小结: android广播原理
1.定义BroadcastReceiver扩展类DataReceiver,实现onReceive方法.
private class DataReceiver extends BroadcastReceiver{//继承自BroadcastReceiver的子类
@Override
public void onReceive(Context context, Intent intent) {
double data = intent.getDoubleExtra("data", 0);
tv.setText("Service的数据为:"+data);
}
}2. 注册广播接收者。注意 "org.yihu.Sample_3_6"广播身份标识,接收时通过此来区分不同广播
dataReceiver = new DataReceiver();
IntentFilter filter = new IntentFilter();//创建IntentFilter对象
filter.addAction("org.yihu.Sample_3_6");
3.发送广播.
Intent intent = new Intent();//创建Intent对象
intent.setAction("org.yihu.Sample_3_6");
intent.putExtra("data", Math.random());
sendBroadcast(intent);//发送广播
4.广播不用后记得注销
unregisterReceiver(dataReceiver);//取消注册Broadcast Receiver
标签:界面,UI,new,import,android,intent,public,Intent From: https://blog.51cto.com/u_3124497/6914126