0

I've tried:

This which doesn't work on my phone: Trying to start a service on boot on Android

This which also fails to work properly: http://www.compiletimeerror.com/2014/12/android-autostart-app-after-boot-with.html#.VpL6sxWLTIU

And a few others which I couldn't find again. Could someone please post a working example of code which will start a MainActivity with the default HelloWorld xml layout?

Code:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />

    <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=".MyBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>

        <activity
            android:name=".MyService"
            android:label="@string/title_activity_my_service" >
        </activity>
    </application>

</manifest>

package com.example;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;

public class MyBroadcastReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        Intent startServiceIntent = new Intent(context, MyService.class);
        context.startService(startServiceIntent);
    }
}

package com.example;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class MyService extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_service);
    }
}
Cœur
  • 37,241
  • 25
  • 195
  • 267
  • what exactly is that you want to do? – OBX Jan 11 '16 at 01:19
  • The first link has a correct example. Post your code, and we'll try to help you with your specific problems, instead of repeating the code already demonstrated. – Mike M. Jan 11 '16 at 01:20
  • I've pretty much already said and from the links you should be able to tell. I want to run an android app upon starting up an android phone. – user5770951 Jan 11 '16 at 01:21
  • 1
    `MyService` is actually an `Activity`, so you need to use the `startActivity()` method in the Receiver, instead of `startService()`. (You'll probably want to rename `MyService`, as well, to avoid confusion.) You'll also need to set `FLAG_ACTIVITY_NEW_TASK` in the `Intent`, as shown in [this post](http://stackoverflow.com/a/8211921). – Mike M. Jan 11 '16 at 01:40
  • @MikeM. I changed the code in `onReceive` to `Intent i = new Intent(context, MyService.class);` `i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);` `context.startActivity(i);` but it still doesn't work. – user5770951 Jan 11 '16 at 01:46
  • Add an `` element to the `` element in the manifest, as shown [here](http://stackoverflow.com/questions/3631982/change-applications-starting-activity), and run your app at least once from the launcher after installation to bring it out of the _stopped_ state. – Mike M. Jan 11 '16 at 01:52
  • 1
    @MikeM. Yes that worked! Thanks. – user5770951 Jan 11 '16 at 02:31

1 Answers1

0

First:create a BroadcastReceiver, onReceive(Context context, Intent intent),start the app you want to startup in this method

    public class BootBroadcastReceiver extends BroadcastReceiver { 
   @Override  
   public void onReceive(Context context, Intent intent) {  
       xxx.class is the app you want to start
       Intent service = new Intent(context,XXXclass);  
       context.startService(service);  
       Log.v("TAG", "you want to startup this app.....");          


  Intent intent = getPackageManager().getLaunchIntentForPackage(packageName); 

context.startActivity(intent );

   }     } 

Secondly:configure receiver and intent-filter

<receiver android:name="BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</receiver>

THirdly,add permission

 <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
杨东冀
  • 1
  • 2