Android組件間的相互調用

來源:互聯網
上載者:User

標籤:android   des   blog   http   java   color   

我們研究兩個問題,
1、Service如何通過Broadcaster更改activity的一個TextView。
(研究這個問題,考慮到Service從伺服器端獲得訊息之後,將msg返回給activity)

2、Activity如何通過Binder調用Service的一個方法。
(研究這個問題,考慮到與伺服器端互動的動作,打包至Service,Activity只呈現介面,調用Service的方法)

結構圖見如下:

如下:

點擊“start service”按鈕,啟動Service,然後更改Activity的UI。

點擊“send msg to server”按鈕調用Service的方法,顯示NotificationBar

代碼

Myservice的:

package com.bankcomm.test;

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() {
sendMsgtoActivity("Service is oncreating.\n");
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
String msg = "Activity is sendding message to service,\n Service send msg to server!\n";
sendMsgtoActivity(msg);
return binder;
}
private void sendMsgtoActivity(String msg){
Intent intent = new Intent("com.android.Hou.msg");
intent.putExtra("msg", msg);
this.sendBroadcast(intent);

}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();

if (notificationManager!= null) {
notificationManager.cancel(0);
notificationManager= null;

}
}
private void showNotification(String msg) {
notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// 定義Notification的各種屬性
Notification notification =new Notification(R.drawable.ic_launcher,
"A Message Coming!", System.currentTimeMillis());
//FLAG_AUTO_CANCEL 該通知能被狀態列的清除按鈕給清除掉
//FLAG_NO_CLEAR 該通知不能被狀態列的清除按鈕給清除掉
//FLAG_ONGOING_EVENT 通知放置在正在運行
//FLAG_INSISTENT 是否一直進行,比如音樂一直播放,知道使用者響應
notification.flags |= Notification.FLAG_ONGOING_EVENT; // 將此通知放到通知欄的"Ongoing"即"正在運行"組中
notification.flags |= Notification.FLAG_NO_CLEAR; // 表明在點擊了通知欄中的"清除通知"後,此通知不清除,經常與FLAG_ONGOING_EVENT一起使用
notification.flags |= Notification.FLAG_SHOW_LIGHTS;
//DEFAULT_ALL 使用所有預設值,比如聲音,震動,閃屏等等
//DEFAULT_LIGHTS 使用預設閃光提示
//DEFAULT_SOUNDS 使用預設提示聲音
//DEFAULT_VIBRATE 使用預設手機震動,需加上<uses-permission android:name="android.permission.VIBRATE" />許可權
notification.defaults = Notification.DEFAULT_LIGHTS;
//疊加效果常量
//notification.defaults=Notification.DEFAULT_LIGHTS|Notification.DEFAULT_SOUND;
notification.ledARGB = Color.BLUE;
notification.ledOnMS =5000; //閃光時間,毫秒

// 設定通知的事件訊息
//Intent notificationIntent =new Intent(MainActivity.this, MainActivity.class); // 點擊該通知後要跳轉的Activity
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class); // 載入類,如果直接通過類名,會在點擊時重新載入頁面,無法恢複最後頁面狀態。
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擷取資訊
*/
public void receiverMsgtoActivity(String msg){
sendMsgtoActivity("\n receiverMsgtoActivity:"+msg);
}
public void sendMsgtoServer(String msg){
showNotification(msg);
}
public class LocalBinder extends Binder{
public Myservice getService(){
return Myservice.this;
}
}

}

MainActivity的:

package com.bankcomm.test;

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.view.View.OnClickListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {
private TextView txtMsg;
private String msg = "";
private UpdateReceiver receiver;
private Myservice myservice;
private final static String TAG = MainActivity.class.getSimpleName();

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtMsg = (TextView) findViewById(R.id.txtMsg);
this.findViewById(R.id.btnsend).setOnClickListener(this);
this.findViewById(R.id.btnstart).setOnClickListener(this);

// 訂閱廣播
receiver = new UpdateReceiver();
IntentFilter filter = new IntentFilter();
filter.addAction("com.android.Hou.msg");
this.registerReceiver(receiver, filter);

}

public class UpdateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// 擷取service傳過來的資訊
msg = intent.getStringExtra("msg");
System.out.println("msg=============="+msg);
txtMsg.append(msg);
}

}

private ServiceConnection conn = new ServiceConnection() {

@Override
public void onServiceDisconnected(ComponentName name) {
// TODO Auto-generated method stub
myservice = null;
}

@Override
public void onServiceConnected(ComponentName name, IBinder service) {
// TODO Auto-generated method stub
myservice = ((Myservice.LocalBinder) service).getService();
Log.i(TAG, "onServiceConnected myService: " + myservice);
}
};

@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();

//結束service
if (conn!=null) {
unbindService(conn);
myservice=null;
}
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this, Myservice.class);
int id = v.getId();
switch (id) {
case R.id.btnstart:
//判斷服務是否啟動
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:
//判斷服務是否啟動
if(false==isServiceRunning(this, Myservice.class.getName())){
Log.i(TAG, "start "+Myservice.class.getSimpleName()+" service");
//啟動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); //第一次啟動服務時此處為null(小編認為雖然服務已啟動成功,但是還沒全部初始化)

if (myservice!=null) {
myservice.sendMsgtoServer("i am sending msg to server");
myservice.receiverMsgtoActivity("this is a msg");

}
break;
default:
break;
}

}
/**
* 判斷服務是否正在運行
*
* @param context
* @param className 判斷的服務名字:包名+類名
* @return true在運行 false 不在運行
*/
public static boolean isServiceRunning(Context context, String className) {
boolean isRunning = false;
ActivityManager activityManager = (ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE);
// 擷取所有的服務
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;
}

}

 

MyBroadcastreceiver的:

package com.bankcomm.test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyBroadcastreceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
Intent service = new Intent(context,Myservice.class);
System.out.println("broadcastreceiver======");
context.startService(service);
}

}

 

布局和設定檔資訊:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/txtMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="server"
/>

<Button
android:id="@+id/btnstart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="start service" />

<Button
android:id="@+id/btnsend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="send msg to server" />

</LinearLayout>

 

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bankcomm.test"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.bankcomm" />

<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<uses-library android:name="android.test.runner" />
<activity
android:name="com.bankcomm.test.MainActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".Myservice"></service>
<receiver android:name=".MyBroadcastreceiver"></receiver>
</application>

</manifest>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.