Android Manifest xml service android name Service Sample

  • Slides: 10
Download presentation
7-3 サービスの連携 1.サービスとは 画面を持たずに処理を行う【Android. Manifest. xmlへの追加】 … <service android: name=“. Service. Sample” /> </application>

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

5.プログラム例 Service. Activityクラス(その1) package jp. service; import java. util. *; import android. app. *;

5.プログラム例 Service. Activityクラス(その1) package jp. service; import java. util. *; import android. app. *; import android. content. *; import android. os. *; import android. view. View. *; import android. widget. *; public class Service. Activity extends Activity { Button bt 1, bt 2; /** Called when the activity is first created. */ @Override public void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); Linear. Layout LL=new Linear. Layout(this); LL. set. Orientation(Linear. Layout. VERTICAL); set. Content. View(LL);

Service. Activityクラス(その2) bt 1=new Button(this); bt 2=new Button(this); bt 1. set. Text("開始"); bt 2.

Service. Activityクラス(その2) bt 1=new Button(this); bt 2=new Button(this); bt 1. set. Text("開始"); bt 2. set. Text("停止"); LL. add. View(bt 1); LL. add. View(bt 2); bt 1. set. On. Click. Listener(new Click. Listener. Proc()); bt 2. set. On. Click. Listener(new Click. Listener. Proc()); } class Click. Listener. Proc implements On. Click. Listener{ public void on. Click(View v){ Context context=get. Application. Context(); Intent intent=new Intent(context, Service. Sample. class); Pending. Intent p. Intent=Pending. Intent. get. Service(context, 0, intent, 0); Alarm. Manager a. Man = (Alarm. Manager)context. get. System. Service(Context. ALARM_SERVICE); if(v==bt 1){ long time=Calendar. get. Instance(). get. Time. In. Millis(); a. Man. set. Repeating(Alarm. Manager. RTC_WAKEUP, time, 10*1000, p. Intent); } else if(v==bt 2){ a. Man. cancel(p. Intent); stop. Service(intent); } } }

Service. Sampleクラス(その1) package jp. service; import java. util. *; import android. app. *; import

Service. Sampleクラス(その1) package jp. service; import java. util. *; import android. app. *; import android. os. *; import android. content. *; import android. widget. *; public class Service. Sample extends Service { Notification. Manager n. Man; Random r; String[] str={"Good morning!", "Good afternoon!", "Good night!", "Good by!", "See again!", "Nice to meet you!", "Thank you!", "Congratulations!"}; public IBinder on. Bind(Intent it){ return null; } public void on. Create() { n. Man=(Notification. Manager)this. get. System. Service(Context. NOTIFICATION_SERVICE); r=new Random(); }

Service. Sampleクラス(その2) public void on. Start(Intent intent, int id){ Notification ntf= new Notification(R. drawable.

Service. Sampleクラス(その2) public void on. Start(Intent intent, int id){ Notification ntf= new Notification(R. drawable. ic_launcher, "Service Sample", System. current. Time. Millis()); Intent i=new Intent(this, Service. Activity. class); Pending. Intent p. Intent= Pending. Intent. get. Activity(this, 0, i, 0); ntf. set. Latest. Event. Info(get. Application. Context(), "Service Sample", "設定画面に移動します", p. Intent); n. Man. notify(0, ntf); int m=r. next. Int(str. length); Toast. make. Text(this, str[m], Toast. LENGTH_LONG). show(); } public void on. Destroy(){ n. Man. cancel(0); } }