トップ 差分 一覧 ソース 置換 検索 ヘルプ PDF RSS ログイン

Android 起動時にサービスを起動する

 Android 起動時にアプリを起動させる

起動時にサービスとしてアプリを実行する。

StartServiceTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
  package test.startservice;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.widget.*;
  import android.view.*;
  import android.content.Intent;
  
  public class StartServiceTest extends Activity  
  {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      initComponent();
    }
    
    private void initComponent(){
      EventProcessor ep = new EventProcessor();
      Button start_button = (Button)findViewById(R.id.start_button);
      Button stop_button = (Button)findViewById(R.id.stop_button);
      start_button.setOnClickListener(ep);
      stop_button.setOnClickListener(ep);
    }
    
    
    private class EventProcessor implements View.OnClickListener {
      public void onClick(View v){
        if ("start_button".equals(v.getTag())){
          System.out.println(v.getTag());
          Intent i = new Intent(StartServiceTest.this, TestService.class);
          StartServiceTest.this.startService(i);
        } else if ("stop_button".equals(v.getTag())){
          System.out.println(v.getTag());
          Intent i = new Intent(StartServiceTest.this, TestService.class);
          StartServiceTest.this.stopService(i);
        }
        
      }
    }
  }

Receiver.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  package test.startservice;
  
  import android.content.*;
  import android.content.Intent;
  import android.content.BroadcastReceiver;
  
  public class Receiver extends BroadcastReceiver  
  {
    @Override  
    public void onReceive(Context context, Intent intent) {
      System.out.println("on Receive");
      // Intent i = new Intent(context, app9.class); 
      // i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      // context.startActivity(i);
      Intent i = new Intent(context, TestService.class);
      context.startService(i);
      
    }
    
  }

TestService.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
  package test.startservice;
  
  import android.app.Service;
  import android.content.Intent;
  import android.os.IBinder;
  import android.widget.Toast;
  
  public class TestService extends Service  
  {
    @Override
    public IBinder onBind(Intent intent) {
      System.out.println("onBind");
      return null;
    }
    
    @Override
    public void onCreate() {
      super.onCreate();
      // Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();
      System.out.println("Service Created");
    }
    
    @Override
    public void onDestroy() {
      super.onDestroy();
      // Toast.makeText(this, "Service Destroyed", Toast.LENGTH_LONG).show();
      System.out.println("Service Destroyed");
    }
    
    @Override
    public void onStart(Intent intent, int startId) {
      super.onStart(intent, startId);
      // Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
      System.out.println("Service Started");
    }
  }

AndroidManifest.xml

 <?xml version="1.0" encoding="utf-8"?>
 <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="test.startservice" android:versionCode="1" android:versionName="1.0">
   <application android:label="@string/app_name">
     <activity android:name="StartServiceTest" android:label="@string/app_name">
       <intent-filter>
         <action android:name="android.intent.action.MAIN" />
         <category android:name="android.intent.category.LAUNCHER" />
       </intent-filter>
     </activity>
 
     <service android:name="test.startservice.TestService" />
     <receiver android:name="test.startservice.Receiver">
       <intent-filter>
         <action android:name="android.intent.action.BOOT_COMPLETED"/>
         <category android:name="android.intent.category.DEFAULT" />
       </intent-filter>
     </receiver>
 
   </application>
   <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
 </manifest> 


res/layout/main.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   >
   <Button  
   android:id="@+id/start_button"
   android:tag="start_button"
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:text="Start Serviece"
   />
   <Button  
   android:id="@+id/stop_button"
   android:tag="stop_button"
   android:layout_width="fill_parent" 
   android:layout_height="wrap_content" 
   android:text="Stop Serviece"
   />
 </LinearLayout>



[カテゴリ: プログラミング言語 > Java > Android]

[通知用URL]



  • Hatenaブックマークに追加
  • livedoorクリップに追加
  • del.icio.usに追加
  • FC2ブックマークに追加

最終更新時間:2011年10月27日 21時30分24秒