2012年12月8日 星期六

Start service at boot time for android 4.0


I can't not start service when I want to start service at boot time for android 4.0, in android. My code is below:
> public class StartUpReceiver extends BroadcastReceiver{   @Override
> public void onReceive(Context context, Intent intent) { String
> action = intent.getAction(); if
> (action.equalsIgnoreCase("android.intent.action.BOOT_COMPLETED")) {
> Intent myIntent = new Intent(context, StartAUT_Service.class);
> Log.i("Broadcast", "startService on boot time:." + myIntent);
> context.startService(myIntent); }
> } }



<uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
> <application android:icon="@drawable/icon" android:label="@string/app_name">
> <receiver android:name="com.Android.Exercise.StartUpReceiver" android:exported="false">
> <intent-filter>
> <action android:name="android.intent.action.BOOT_COMPLETED"/>
> <action android:name="StartInstrument" />
> <action android:name="PrintControlName" />
> </intent-filter>
> </receiver>
> <service android:enabled="true" android:name="StartAUT_Service">
> <intent-filter>
> <action android:name="com.Android.Exercise.StartAUT_Service" />
> </intent-filter>
> </service>
> </application>
And in LogCat show Shutting down VM when i run above project, so my broadcast don't receive action. Plz help me.!!! Thanks so much.
share|improve this question
feedback


In your onResume method of a BroadCastReceiver class put following code
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
super.onReceive(context, intent);

if(intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)){
Intent i = new Intent(context, yourService.class);
i
.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context
.startService(i);

}
}
share|improve this answer


<receiver
            android:name="boottostart" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

沒有留言:

張貼留言

Related Posts Plugin for WordPress, Blogger...