7-3 サービスの連携 1.サービスとは

7-3 サービスの連携
1.サービスとは
画面を持たずに処理を行う【AndroidManifest.xmlへの追加】
…
<service android:name=“.ServiceSample” />
</application>
</manifest>
Intent it = new Intent(context, ServiceSample.class);
it.startService();
2.ペンディングインテントの使用
サービスを後で起動するためのペンディングインテント
(Pending Intent)クラスを用いる。
インテントを作成した上で,
これをベンディングインテントに渡す。
PendingIntent pIntent =
PendingIntent.getService(context,0,it,0);
3.アラームマネージャの使用
後でインテントを起動するためのアラームマネージャ
(AlarmManager)
①一定時間後にインテントを発行する。
②インテントを繰り返す。
アラームマネージャの指定方法
①アラームマネージャを取得。
②アラームマネージャでインテントを発行する時間間隔等を
指定する。
③デストロイの状態でアラームマネージャをキャンセルする。
4.通知領域の指定
サービスを利用する場合,他のアプリケーションや
ホーム画面に移動しても処理を続ける。
他の画面に移動した場合,サービス停止の方法を
用意しておく必要がある。
通知領域(ノーティフィケーション)を使う。
5.プログラム例
ServiceActivityクラス(その1)
package jp.service;
import java.util.*;
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;
public class ServiceActivity extends Activity {
Button bt1, bt2;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout LL=new LinearLayout(this);
LL.setOrientation(LinearLayout.VERTICAL);
setContentView(LL);
ServiceActivityクラス(その2)
bt1=new Button(this); bt2=new Button(this);
bt1.setText("開始"); bt2.setText("停止");
LL.addView(bt1);
LL.addView(bt2);
bt1.setOnClickListener(new ClickListenerProc());
bt2.setOnClickListener(new ClickListenerProc());
}
class ClickListenerProc implements OnClickListener{
public void onClick(View v){
Context context=getApplicationContext();
Intent intent=new Intent(context, ServiceSample.class);
PendingIntent pIntent=PendingIntent.getService(context, 0, intent, 0);
AlarmManager aMan =
(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
if(v==bt1){
long time=Calendar.getInstance().getTimeInMillis();
aMan.setRepeating(AlarmManager.RTC_WAKEUP, time,10*1000,pIntent);
}
else if(v==bt2){ aMan.cancel(pIntent); stopService(intent);}
}
}
ServiceSampleクラス(その1)
package jp.service;
import java.util.*;
import android.app.*;
import android.os.*;
import android.content.*;
import android.widget.*;
public class ServiceSample extends Service {
NotificationManager nMan; Random r;
String[] str={"Good morning!","Good afternoon!","Good night!","Good by!",
"See again!","Nice to meet you!","Thank you!","Congratulations!"};
public IBinder onBind(Intent it){
return null;
}
public void onCreate() {
nMan=(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
r=new Random();
}
ServiceSampleクラス(その2)
public void onStart(Intent intent, int id){
Notification ntf= new Notification(R.drawable.ic_launcher,
"Service Sample",System.currentTimeMillis());
Intent i=new Intent(this, ServiceActivity.class);
PendingIntent pIntent= PendingIntent.getActivity(this, 0,i,0);
ntf.setLatestEventInfo(getApplicationContext(),"Service Sample",
"設定画面に移動します",pIntent);
nMan.notify(0,ntf);
int m=r.nextInt(str.length);
Toast.makeText(this, str[m],Toast.LENGTH_LONG).show();
}
public void onDestroy(){
nMan.cancel(0);
}
}
起動直後
ServiceActivity
「新規」ボタンクリック Term1, Term2に入力
「OK」ボタンクリック
ServiceSample
6.実行例
ServiceActivityに戻って
Term1の内容を表示
何回か繰り返し