2011年6月1日水曜日

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

起動時にサービスとしてアプリを実行する。
StartServiceTest.java
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

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


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>

0 件のコメント:

コメントを投稿