부팅시 서비스 실행하는 코드 짜봤습니다.. 리시버가 호출이 안되네요..
Activity는 없는 서비스만 실행되는 코드입니다.
Activity 있을때는 리시버가 호출이 되지만 Activity가 없으니 리시버가 호출이 안되네요..
왜그런지 아시는분 답변좀... ㅠㅠ
//메니페스트
package="jyj.jyj.receiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver android:name="jyj.jyj.receiver.Receiver" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
<service android:name="jyj.jyj.receiver.MyService" >
</service>
</application>
</manifest>
//브로드케스트리시버 자바
package jyj.jyj.receiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("JYJ_TEST", "onReceive : BOOT_COMPLETED");
Toast.makeText(context, "onReceive : BOOT_COMPLETED", Toast.LENGTH_LONG).show();
if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
context.startService(new Intent(context, MyService.class));
}
}
}
//서비스 자바
package jyj.jyj.receiver;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class MyService extends Service {
@Override
public void onCreate() {
Toast.makeText(getBaseContext(), "써비스 시작ㅋㅋ", Toast.LENGTH_LONG).show();
super.onCreate();
}
@Override
@Deprecated
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
super.onStart(intent, startId);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}