Intent的另一种用途是发送广播消息,应用程序和Android系统都可以使用Intent发送广播消息,广播消息的内容可以是与应用程序密切相关的数据信息,也可以是Android的系统信息,例如网络连接变化、电池电量变化等。如果应用程序注册了BroadcastReceiver,则可以接收到指定的广播消息。
创建一个Intent,调用sendBroadcast()函数就可把Intent携带的信息广播出去,还可以用Intent的putExtra()方法传递额外数据。
接收广播消息需要继承BroadcastReceiver类,并重写onReceive()方法。
当系统接收到与注册的BroadcastReceiver组件相匹配的广播消息时,系统会调用MyReceiver来接收广播消息,onReceive()方法会被执行
创建Intent,将广播消息放进去,调用sendBroadcast方法发送intent
Intent intent = new Intent("www.broad.com");
intent.putExtra("BroadTest","TestInfo");
sendBroadcast(intent);
创建一个BroadcastReceiver类,重写onReceive方法 ,显示广播消息
public class MyReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO: This method is called when the BroadcastReceiver is receiving
// an Intent broadcast.
//使用Toast将消息解析出来
Toast.makeText(context,intent.getStringExtra("BroadTest"),Toast.LENGTH_LONG).show();
}
}
以绑定方式使用Service,能够获取到Service实例,不仅能够正常启动Service,还能够调用Service中的公有方法和属性为了使Service支持绑定,需要在Service类中重写onBind()方法,并在onBind()方法中返回Service实例
private LocalBinder myBinder = new LocalBinder();
public class LocalBinder extends Binder {
public MyService getService(){
return MyService.this;
}
}
@Override
public IBinder onBind(Intent intent) { //绑定方式使用服务
// TODO: Return the communication channel to the service.
return myBinder;
}
绑定方式中,当调用者通过bindService()绑定Service时,onCreate()和onBind()先后被调用当调用者通过unbindService()取消绑定Servcie时,onUnbind()被调用。如果onUnbind()返回true,则表示重新绑定服务时,onRebind()将被调用。
@Override
public boolean onUnbind(Intent intent) {
Toast.makeText(this, "onUnbind", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public void onRebind(Intent intent) {
super.onRebind(intent);
Toast.makeText(this,"onRebind",Toast.LENGTH_SHORT).show();
}
通过bindService()函数绑定服务
例
MainActivity
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.net.Uri;
import android.os.Bundle;
import android.os.IBinder;
import android.text.Editable;
import android.text.TextPaint;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private MyService myService = null; //初始化服务变量
private boolean isBind = false;
private static TextView textView;
private ServiceConnection myConnection = new ServiceConnection() {
@Override //重写onServiceConnection方法,通过getService获取服务
public void onServiceConnected(ComponentName name, IBinder service) {
myService = ((MyService.LocalBinder) service).getService();
isBind = true;
}
@Override
public void onServiceDisconnected(ComponentName name) { //设置标记符用来判断状态
isBind = false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.ResultTextView);
//final Intent intent = new Intent(MainActivity.this,MyService.class); //显示启动
final Intent intent = new Intent("www.cczu.service"); //隐式启动
intent.setPackage("com.example.myapplication01");
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) { //bindService将intent和连接绑定
bindService(intent,myConnection,BIND_AUTO_CREATE);
}
});
final Button StopButton = findViewById(R.id.StopServiceBTN); //通过这个按钮结束服务
StopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
stopService(intent);
if (isBind){ //调用unbindService方法取消连接,并设置isBind标记为false
unbindService(myConnection);
isBind = false;
}
}
});
final Button AddButton = findViewById(R.id.AddBtn); //调用服务类中的方法查看效果
AddButton.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) { //已经绑定则进行运算
if(isBind){
long a = Math.round(Math.random()*100);
long b = Math.round(Math.random()*100);
long r = myService.Add(a,b);
textView.setText(a +"+"+ b + "=" + r);
}else {
textView.setText("unbind"); //否则就是没有绑定
}
}
});
}
}
MyService:
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
public MyService() {
}
private LocalBinder myBinder = new LocalBinder();
public class LocalBinder extends Binder { //初始化服务对象
public MyService getService(){
return MyService.this;
}
}
@Override
public IBinder onBind(Intent intent) { //绑定方式使用服务
// TODO: Return the communication channel to the service.
return myBinder;
}
@Override
public boolean onUnbind(Intent intent) { //取消绑定
Toast.makeText(this, "onUnbind", Toast.LENGTH_SHORT).show();
return true;
}
@Override
public void onRebind(Intent intent) { //重新绑定intent
super.onRebind(intent);
Toast.makeText(this,"onRebind",Toast.LENGTH_SHORT).show();
}
@Override
public void onCreate() { //创建服务
super.onCreate();
Toast.makeText(this,"onCreate",Toast.LENGTH_SHORT).show();
}
public long Add(long a, long b){ //运算结果返回给主界面
return a + b;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this,"onStartCommand",Toast.LENGTH_SHORT).show();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
Toast.makeText(this,"onDestroy",Toast.LENGTH_SHORT).show();
}
}