1 / 10

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 )クラスを用いる。.

alissa
Télécharger la présentation

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

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 7-3 サービスの連携1.サービスとは 画面を持たずに処理を行う【AndroidManifest.xmlへの追加】 … <service android:name=“.ServiceSample” /> </application> </manifest> Intent it = new Intent(context, ServiceSample.class); it.startService();

  2. 2.ペンディングインテントの使用 サービスを後で起動するためのペンディングインテント(Pending Intent)クラスを用いる。 インテントを作成した上で, これをベンディングインテントに渡す。 PendingIntent pIntent = PendingIntent.getService(context,0,it,0);

  3. 3.アラームマネージャの使用 後でインテントを起動するためのアラームマネージャ(AlarmManager) ①一定時間後にインテントを発行する。 ②インテントを繰り返す。

  4. アラームマネージャの指定方法 ①アラームマネージャを取得。 ②アラームマネージャでインテントを発行する時間間隔等を指定する。 ③デストロイの状態でアラームマネージャをキャンセルする。

  5. 4.通知領域の指定 サービスを利用する場合,他のアプリケーションや ホーム画面に移動しても処理を続ける。 他の画面に移動した場合,サービス停止の方法を 用意しておく必要がある。 通知領域(ノーティフィケーション)を使う。

  6. 5.プログラム例ServiceActivityクラス(その1)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);

  7. 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);} } }

  8. 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(); }

  9. 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); } }

  10. 起動直後 ServiceActivity Term1, Term2に入力 「OK」ボタンクリック 「新規」ボタンクリック ServiceSample 6.実行例 ServiceActivityに戻って Term1の内容を表示 何回か繰り返し

More Related