automatically run an application on Android phone

2019-01-26 02:36发布

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 class MyStartUpReciever extends BroadcastReceiver{

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO Auto-generated method stub
        Intent ii = new Intent();
        ii.setAction("com.sat.servicetrack");
        context.startService(ii);
    }


}

and this is my manifest.xml

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

<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".ServiceTrack"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
<receiver android:name=".MyStartupReciever">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
        <category android:name="android.intent.category.HOME" />
    </intent-filter>
</receiver>
<service android:enabled="true" android:name=".MyService" >
    <intent-filter>
        <action android:name="com.sat.servicetrack" />
    </intent-filter>
</service>
</application>

Am I missing anything?

4条回答
来,给爷笑一个
2楼-- · 2019-01-26 02:50
    @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

查看更多
小情绪 Triste *
3楼-- · 2019-01-26 02:58

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

查看更多
迷人小祖宗
4楼-- · 2019-01-26 03:00

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

查看更多
老娘就宠你
5楼-- · 2019-01-26 03:03

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>
查看更多
登录 后发表回答