總網頁瀏覽量

2013年4月22日 星期一

Service

Service:
播歌、下載檔案、電池快耗盡通知、mail來電通知

Service通常會啟動另外一個thread處理較花時間的程序


一、Notification,通知服務的功能實作(有部分method是deprecation)

1.在MianActivity宣告intent並startService

Intent it = new Intent(this,MyService.class);
it.putExtra("Name", "Morzart");
startService(it);


2.新增class繼承extends Service
extends Service

3.
務必記得去Mainifest.xml
註冊Service不然是不會執行Service的

4.
override    onStartCommand(Intent intent)。
//接收啟動Service的Activity Inent物件
//如果沒有特別事情,可以省略

public int onStartCommand(Intent intent, int flags, int startId) {
String name = intent.getStringExtra("Name");
Log.i("YYP",name);
//Toast.makeText(this, name, Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}




宣告
private Timer timer;

在onCreate(){
timer= new Timer();
ttimer.scheduel(new MyNotice(),10000);

}
//我們會發現當啟動Service之後會先onCreate 接著再執行onStartCommand


//接著宣告thread處理service要做的事情
//本例子是在state bar顯示通知
//使用者按下通知時候會跳到另一個頁面

private class MyNotice extends TimerTask{

public void run(){
NotificationManager mgr= (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
//notificationManager負責整個系統通知控制
//NotificationManager屬於系統管理類別因此要從getSystemService取得notification服務
@SuppressWarnings("deprecation")
Notification notice = new Notification(R.drawable.ic_launcher,"重要通知",System.currentTimeMillis());
//notification 物件此物件名稱是"重要通知"
//(顯示在state Bar的icon,顯示再狀態bar的文字,顯示的時戳)
//System.currentTimeMillis()取得現在時間


notice.flags=Notification.FLAG_AUTO_CANCEL;
//按一下service後notification會Service列表消失
// notice.flags=Notification.FLAG_FOREGROUND_SERVICE;
//Service為常駐狀態,常駐在service列表
// notice.flags=Notification.FLAG_ONLY_ALERT_ONCE;
//Service為常駐狀態,常駐在service列表

//flag表用來標記notification是何種類型任務


Intent it = new Intent(this,Page2.class);
//按下Service通知 進入page2
PendingIntent pit = PendingIntent.getActivity(this, 1, it, 0);
// PendingItent pit = PendingIntent.getActivities(context, requestCode, intents, flags);
//pending:即將發生的。
//顧名思義是宣告暫時存放的intent
//PendingIntent pit = PendingIntent.getBroadcast(this,1, it, 0);
//PendingIntent pit = PendingIntent.getService(this,1, it, 0);


notice.setLatestEventInfo(this, "重要訊息", "好好學習天天向上", pit);
//deprecated
//notice.setLatestEventInfo(context, contentTitle, contentText, contentIntent)
//當使用者點下該notification之後會去執行pit的intent

mgr.notify(1, notice);
//指定要顯示的notification物件以及識別ID
}
}



最後要停止Service,要宣告intent物件
再呼叫stopService()

private void toStop(){
Intent it = new Intent(this,MyService.class);
stopService(it);
//執行到這,會呼叫Serivce的onDestroy()
//雖然stop了 但是service仍會在service列表裡
}



二、廣播訊息、接收廣播訊息

在Service的.java檔
實作發送廣播訊息,以TimerTask實作之


timer.schedule(new MyTask(), 0,500);



private class MyTask extends TimerTask{

@Override
public void run() {

//Toast.makeText(this, "service start", Toast.LENGTH_SHORT).show();
//要Handler

int temp=(int)(Math.random()*49+1);
Intent it = new Intent("MyAction");
//Intent(String Action)  給intent貼標籤"MyAction"
it.putExtra("value", temp);//想要傳遞的東西放進去
sendBroadcast(it);//廣播出去由BroadcastReceiver接收
}
}

在MainActivity裡面:
實作接收訊息

宣告
private class MyReceiver extends BroadcastReceiver{


@Override
public void onReceive(Context arg0, Intent intent) {
int value = intent.getIntExtra("value", -1);
//int value = intent.getIntExtra("", defaultValue)

tv.setText(""+value);
}



}


宣告:

private MyReceiver receiver;

receiver = new MyReceiver();

IntentFilter filter = new IntentFilter("MyAction");
//只接收"MyAction"的intent廣播

//IntentFilter filter = new IntentFilter("MyAction2");
//如果這裡改為"MyAction2"會收不到MyAction的intent廣播


registerReceiver(receiver,filter);


沒有留言:

張貼留言