Android實(shí)現(xiàn)Activity、Service與Broadcaster三大組件之間互相調(diào)用的方法詳解
本文實(shí)例講述了Android實(shí)現(xiàn)Activity、Service與Broadcaster三大組件之間互相調(diào)用的方法。分享給大家供大家參考,具體如下:
我們研究?jī)蓚€(gè)問(wèn)題,
1、Service如何通過(guò)Broadcaster更改activity的一個(gè)TextView。
(研究這個(gè)問(wèn)題,考慮到Service從服務(wù)器端獲得消息之后,將msg返回給activity)
2、Activity如何通過(guò)Binder調(diào)用Service的一個(gè)方法。
(研究這個(gè)問(wèn)題,考慮到與服務(wù)器端交互的動(dòng)作,打包至Service,Activity只呈現(xiàn)界面,調(diào)用Service的方法)
結(jié)構(gòu)圖見(jiàn)如下:

效果圖如下:

點(diǎn)擊“start service”按鈕,啟動(dòng)Service,然后更改Activity的UI。

點(diǎn)擊“send msg to server”按鈕調(diào)用Service的方法,顯示NotificationBar
代碼:
1、新建一個(gè)MyService類,繼承Service
package com.ljq.activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Binder;
import android.os.IBinder;
public class MyService extends Service {
private NotificationManager notificationManager = null;
private final IBinder binder = new LocalBinder();
@Override
public void onCreate() {
sendMsgtoActivty("Service is oncreating.\n");
}
@Override
public IBinder onBind(Intent intent) {
String msg = "Activity is sendding message to service,\n Service send msg to server!\n";
sendMsgtoActivty(msg);
return binder;
}
/**
* 把信息傳遞給activity
*
* @param msg
*/
private void sendMsgtoActivty(String msg) {
Intent intent = new Intent("com.android.Yao.msg");
intent.putExtra("msg", msg);
this.sendBroadcast(intent);
}
@Override
public void onDestroy() {
super.onDestroy();
if(notificationManager!=null){
notificationManager.cancel(0);
notificationManager=null;
}
}
/**
* 在狀態(tài)欄顯示通知
*
* @param msg
*/
private void showNotification(String msg) {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 定義Notification的各種屬性
Notification notification =new Notification(R.drawable.icon,
"A Message Coming!", System.currentTimeMillis());
//FLAG_AUTO_CANCEL 該通知能被狀態(tài)欄的清除按鈕給清除掉
//FLAG_NO_CLEAR 該通知不能被狀態(tài)欄的清除按鈕給清除掉
//FLAG_ONGOING_EVENT 通知放置在正在運(yùn)行
//FLAG_INSISTENT 是否一直進(jìn)行,比如音樂(lè)一直播放,知道用戶響應(yīng)
notification.flags |= Notification.FLAG_ONGOING_EVENT; // 將此通知放到通知欄的"Ongoing"即"正在運(yùn)行"組中
notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在點(diǎn)擊了通知欄中的"清除通知"后,此通知不清除,經(jīng)常與FLAG_ONGOING_EVENT一起使用
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
//DEFAULT_ALL 使用所有默認(rèn)值,比如聲音,震動(dòng),閃屏等等
//DEFAULT_LIGHTS 使用默認(rèn)閃光提示
//DEFAULT_SOUNDS 使用默認(rèn)提示聲音
//DEFAULT_VIBRATE 使用默認(rèn)手機(jī)震動(dòng),需加上<uses-permission android:name="android.permission.VIBRATE" />權(quán)限
notification.defaults = Notification.DEFAULT_LIGHTS;
//疊加效果常量
//notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND;
notification.ledARGB = Color.BLUE;
notification.ledOnMS =5000; //閃光時(shí)間,毫秒
// 設(shè)置通知的事件消息
//Intent notificationIntent =new Intent(MainActivity.this, MainActivity.class); // 點(diǎn)擊該通知后要跳轉(zhuǎn)的Activity
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class); // 加載類,如果直接通過(guò)類名,會(huì)在點(diǎn)擊時(shí)重新加載頁(yè)面,無(wú)法恢復(fù)最后頁(yè)面狀態(tài)。
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent contentItent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(this, "Message", "Message:" + msg, contentItent);
// 把Notification傳遞給NotificationManager
notificationManager.notify(0, notification);
}
/**
* 從activity獲取信息
*
* @param msg
*/
public void receiverMsgtoActivity(String msg){
sendMsgtoActivty("\n receiverMsgtoActivity:"+msg);
}
public void sendMsgtoServer(String msg) {
showNotification(msg);
}
public class LocalBinder extends Binder {
public MyService getService() {
return MyService.this;
}
}
}
2、新建MyBroadcastreceiver類,繼承BroadcastReceiver,用來(lái)發(fā)送Intent啟動(dòng)服務(wù)
package com.ljq.activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
/**
* 發(fā)送Intent啟動(dòng)服務(wù)
*
* @author jiqinlin
*
*/
public class MyBroadcastreceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Intent service = new Intent(context, MyService.class);
context.startService(service);
}
}
3、新建MainActivity類,其實(shí)是一個(gè)activity,用來(lái)呈現(xiàn)界面
package com.ljq.activity;
import java.util.List;
import android.app.Activity;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener {
private String msg = "";
private TextView txtMsg;
private UpdateReceiver receiver;
private MyService myService;
private final static String TAG=MainActivity.class.getSimpleName();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtMsg = (TextView) this.findViewById(R.id.txtMsg);
this.findViewById(R.id.btnStart).setOnClickListener(this);
this.findViewById(R.id.btnSend).setOnClickListener(this);
//訂閱廣播Intent
receiver = new UpdateReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.android.Yao.msg");
this.registerReceiver(receiver, filter);
//初始化時(shí)啟動(dòng)服務(wù)
//Intent intent = new Intent(MainActivity.this, MyService.class);
//this.bindService(intent, conn, BIND_AUTO_CREATE);
}
@Override
protected void onDestroy() {
super.onDestroy();
//結(jié)束服務(wù)
if(conn!=null){
unbindService(conn);
myService=null;
}
}
public class UpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//獲取service傳過(guò)來(lái)的信息
msg = intent.getStringExtra("msg");
txtMsg.append(msg);
}
}
private ServiceConnection conn = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
myService = ((MyService.LocalBinder) service).getService();
Log.i(TAG, "onServiceConnected myService: "+myService);
}
@Override
public void onServiceDisconnected(ComponentName name) {
myService = null;
}
};
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, MyService.class);
switch (v.getId()) {
case R.id.btnStart:
//判斷服務(wù)是否啟動(dòng)
if(false==isServiceRunning(this, MyService.class.getName())){
Log.i(TAG, "start "+MyService.class.getSimpleName()+" service");
this.bindService(intent, conn, BIND_AUTO_CREATE);
}
Log.i(TAG, MyService.class.getName()+" run status: "+isServiceRunning(this, MyService.class.getName()));
break;
case R.id.btnSend:
//判斷服務(wù)是否啟動(dòng)
if(false==isServiceRunning(this, MyService.class.getName())){
Log.i(TAG, "start "+MyService.class.getSimpleName()+" service");
this.bindService(intent, conn, BIND_AUTO_CREATE);
}
Log.i(TAG, MyService.class.getName()+" run status: "+isServiceRunning(this, MyService.class.getName()));
Log.i(TAG, "onClick myService: "+myService); //第一次啟動(dòng)服務(wù)時(shí)此處為null(小編認(rèn)為雖然服務(wù)已啟動(dòng)成功,但是還沒(méi)全部初始化)
if(myService!=null){
myService.sendMsgtoServer("i am sending msg to server");
//從activity傳遞信息給service
myService.receiverMsgtoActivity("this is a msg");
}
break;
}
}
/**
* 判斷服務(wù)是否正在運(yùn)行
*
* @param context
* @param className 判斷的服務(wù)名字:包名+類名
* @return true在運(yùn)行 false 不在運(yùn)行
*/
public static boolean isServiceRunning(Context context, String className) {
boolean isRunning = false;
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
//獲取所有的服務(wù)
List<ActivityManager.RunningServiceInfo> services= activityManager.getRunningServices(Integer.MAX_VALUE);
if(services!=null&&services.size()>0){
for(ActivityManager.RunningServiceInfo service : services){
if(className.equals(service.service.getClassName())){
isRunning=true;
break;
}
}
}
return isRunning;
}
}
4、main.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"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/txtMsg" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="wrap_content" android:layout_height="wrap_content"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="start service" android:id="@+id/btnStart"/> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="send msg to server" android:id="@+id/btnSend"/> </LinearLayout> </LinearLayout>
5、清單文件AndroidManifest.xml,用來(lái)配置組件等信息
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ljq.activity"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".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>
<service android:name=".MyService"/>
<receiver android:name=".MyBroadcastreceiver" />
</application>
<uses-sdk android:minSdkVersion="7" />
</manifest>
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android視圖View技巧總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android檢測(cè)Activity或者Service是否運(yùn)行的方法
- Android中Service和Activity相互通信示例代碼
- Android中Service與Activity之間通信的幾種方式
- Android Activity與Service通信(不同進(jìn)程之間)詳解
- Android Activity 與Service進(jìn)行數(shù)據(jù)交互詳解
- 淺談Android Activity與Service的交互方式
- Android使用Messenger實(shí)現(xiàn)service與activity交互
- Android實(shí)現(xiàn)從activity中停止Service的方法
- Android中Service實(shí)時(shí)向Activity傳遞數(shù)據(jù)實(shí)例分析
- android使用service和activity獲取屏幕尺寸的方法
- 詳解Android Service與Activity之間通信的幾種方式
相關(guān)文章
http請(qǐng)求繞過(guò)Filter的實(shí)現(xiàn)實(shí)例
這篇文章主要介紹了http請(qǐng)求繞過(guò)Filter的實(shí)現(xiàn)實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android ScrollView無(wú)法填充滿屏幕的解決辦法
這篇文章主要介紹了Android ScrollView無(wú)法填充滿屏幕的解決辦法的相關(guān)資料,這里提供實(shí)例和解決辦法,需要的朋友可以參考下2017-07-07
Android仿QQ好友列表分組實(shí)現(xiàn)增刪改及持久化
這篇文章主要介紹了Android仿QQ好友列表分組實(shí)現(xiàn)增刪改及持久化的相關(guān)資料,需要的朋友可以參考下2016-01-01
Android Studio使用ViewPager+Fragment實(shí)現(xiàn)滑動(dòng)菜單Tab效果
這篇文章主要為大家詳細(xì)介紹了Android Studio使用ViewPager+Fragment實(shí)現(xiàn)滑動(dòng)菜單Tab效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-09-09
Android自定義ViewGroup實(shí)現(xiàn)側(cè)滑菜單
這篇文章主要為大家詳細(xì)介紹了Android如何通過(guò)自定義ViewGroup實(shí)現(xiàn)側(cè)滑菜單,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下2023-01-01
Android 性能優(yōu)化系列之bitmap圖片優(yōu)化
在日常開(kāi)發(fā)的APP,大部分時(shí)候需要想用戶展示圖片信息,圖片最終對(duì)應(yīng)Android中的Bitmap對(duì)象。而對(duì)于APP端來(lái)說(shuō)Bitmap又是一個(gè)比較麻煩的問(wèn)題,主要表現(xiàn)在Bitmap是非常占用內(nèi)存的對(duì)象,處理不當(dāng)將導(dǎo)致APP運(yùn)行卡頓甚至出現(xiàn)OOM2021-11-11
Android使用AlertDialog實(shí)現(xiàn)對(duì)話框
本文主要介紹了Android使用AlertDialog實(shí)現(xiàn)對(duì)話框的相關(guān)知識(shí),具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-03-03
Flutter?點(diǎn)擊兩次退出app的實(shí)現(xiàn)示例
本文主要介紹了Flutter?點(diǎn)擊兩次退出app的實(shí)現(xiàn)示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05

