使用BootReceiver时,应用程序崩溃启动

时间:2014-09-19 13:01:54

标签: android xml

我希望我的应用程序在启动平板电脑时启动浏览器,但在启动时崩溃。如果有人请让我知道我的错误?我在 activity_main.xmle 中没有写任何内容。它显示了我的消息"不幸的是,应用程序崩溃了#34;。

mainfest

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

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>    
        <receiver android:name=".BootReciever" >
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
            </intent-filter>
        </receiver>            
    </application>        
</manifest>

mainActivitc类

package com.example.autostartmyapp;    

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

public class MainActivity extends Activity {    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://google.com/"));
        startActivity(browserIntent);
    }

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

    }    
}

4 个答案:

答案 0 :(得分:1)

将两个类分开并修复清单,如下所示

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

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="21" />    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>    
        <receiver
        android:name="com.example.autostartmyapp.BootReciever"
        android:enabled="true"         >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />
            <action android:name="android.intent.action.QUICKBOOT_POWERON" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>            
    </application>        
</manifest>

编写MainActivity

MainActivity {
...}

BootReceiver类

package com.example.BootReciever;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class BootReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context ctx, Intent arg1) {

            Intent iMainActivity = new Intent(ctx, yourMainActivityClass);  
            iMainStartActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ctx.startActivity(iMainActivity);
    }

}

答案 1 :(得分:0)

猜测一下,我说它可能是因为你的清单指定.BootReceiver作为接收者,但根据你的代码,BootReceiver类嵌套在MainActivity中。因此,您需要将BootReceiver移动到顶级类,或者更改清单以指定.MainActivity.BootReceiver作为接收方。

如果没有解决它,请发布您的logcat,以便我们可以看到导致崩溃的错误。

答案 2 :(得分:0)

在Manifest中作为接收者部分使用它:

   <receiver
        android:name=".BootReciever"
        android:enabled="true"
        android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </receiver>

并照顾不应该内心的广播接收器类:

package com.example.BootReciever;

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

public class BootReciever extends BroadcastReceiver {

    @Override
    public void onReceive(Context ctx, Intent arg1) {

            Intent iMainActivity = new Intent(ctx, MainActivity.class);  
            iMainStartActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            ctx.startActivity(iMainActivity);
    }



}

答案 3 :(得分:-1)

您的应用程序启动速度非常快,并且不会让其他重要应用程序首先启动我认为您应该在应用程序中添加一些延迟以启动。像这样:

public class BootUpReceiver extends BroadcastReceiver {

@Override

public void onReceive(final Context context, Intent intent) {

    // We can't wait on the main thread as it would be blocked if we wait for too long
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                // Lets wait some time before starting the service to be fair to other processes on startup
                Thread.sleep(5000);
            } catch (InterruptedException e) {
            }

            Intent myIntent = new Intent(context, MainActivity.class);
            myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(myIntent);
        }
    }).start();
}