automatically run an application on Android phone startup

后端 未结 4 1412
一整个雨季
一整个雨季 2020-12-18 16:59

I want to start my application when phone startup

I just follow tutorial from here but it doesn\'t work in my device. Please see my method:

public c         


        
相关标签:
4条回答
  • 2020-12-18 17:32
        @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
                context.startService(new Intent(context, BootService.class));
            }
        }
    

    and next you should implements BootService class extended Service

    0 讨论(0)
  • 2020-12-18 17:42

    I've done something similiar, but I was starting activity. Here is how I done it:

    In Manifest:

    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    

    In Java code:

    public class BootUpReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            PendingIntent i = PendingIntent.getActivity(context, 0, new Intent(
                    context,MainActivity.class),
                    Intent.FLAG_ACTIVITY_NEW_TASK);
            AlarmManager mgr = (AlarmManager) context
                    .getSystemService(Context.ALARM_SERVICE);
            mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 20000, i);
        }
    }
    

    Your code seems to be correct, but try using PendingIntent ;) Hope it helps you

    0 讨论(0)
  • 2020-12-18 17:43

    try like this....

    @Override
        public void onReceive(Context context, Intent intent) {
            // TODO Auto-generated method stub
            Intent i = new Intent(context, BootingActivity.class);
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);
        }
    

    in manifest file...

     <receiver android:name=".BroadcastReceiver">
                <intent-filter>
                    <action android:name="android.intent.action.BOOT_COMPLETED"></action>
                    <category android:name="android.intent.category.DEFAULT"></category>
                </intent-filter>
            </receiver>
    
    0 讨论(0)
  • 2020-12-18 17:43

    You are not calling the Service.

    Code like this.

    Intent objIntent= new Intent(context, MyService.class);
    context.startService(objIntent);
    

    Click Here to know how to start service from broadcast receiver

    0 讨论(0)
提交回复
热议问题