Application is not starting on device boot

2020-03-30 04:32发布

问题:

I used BroadcastReceiver to start my application on boot, but it is not starting

here is my code

Manifest code :

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="9" />
<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="com.copy.MyBroadcastreceiver" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
        </intent-filter>
    </receiver>

    <activity
        android:name="com.copy.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

Broadcast class code:

public class MyBroadcastreceiver extends BroadcastReceiver 
{

@Override
public void onReceive(Context context, Intent intent) 
{
    Intent startServiceIntent = new Intent(context, MainActivity.class);
    startServiceIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startService(startServiceIntent);
}

}

MainActivity class :

public class MainActivity extends Activity 
{
ListView lView;
private ArrayList results = new ArrayList();

@SuppressWarnings("unchecked")
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    lView = (ListView) findViewById(R.id.list1);
    PackageManager pm = this.getPackageManager(); 

    Intent intent = new Intent(Intent.ACTION_MAIN, null);
    intent.addCategory(Intent.CATEGORY_LAUNCHER);


    List<ResolveInfo> list = pm.queryIntentActivities(intent,PackageManager.PERMISSION_GRANTED);
    for (ResolveInfo rInfo : list)
    {
        results.add(rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());       
        Log.w("Installed Applications", rInfo.activityInfo.applicationInfo.loadLabel(pm).toString());
    }
    lView.setAdapter(new ArrayAdapter(this,android.R.layout.simple_list_item_1, results));
    lView.setOnItemClickListener(new OnItemClickListener()
    {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) 
        {               
            String x = (String) arg0.getItemAtPosition(arg2);
            Toast.makeText(getApplicationContext(), "This is "+lView.getItemAtPosition(arg2), Toast.LENGTH_SHORT).show();

        }
    });
}


}

回答1:

You are using startService() and MainActivity is not a Service. You need to use startActivity() instead.

public class MyBroadcastreceiver extends BroadcastReceiver 
{
    @Override
    public void onReceive(Context context, Intent intent) 
    {
        Intent startActivityIntent = new Intent(context, MainActivity.class);
        startActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(startActivityIntent);
    }
}


回答2:

Yes, call startActivity:

@Override
public void onReceive(Context context, Intent intent) {

     Intent startActivityIntent = new Intent(context, MainActivity.class);
        startActivityIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(startActivityIntent);
    }

For More Refer this Link http://blog.burnayev.com/2009/08/android-development-how-to-launch.html



回答3:

Try starting an Activity as opposed to Service:-

Intent i = new Intent(context, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);


标签: android boot