设备启动后我的应用程序无法启动?

时间:2012-06-15 08:22:11

标签: android boot autostart

我已经编写了以下课程来开始我的应用活动Home.class,但在设备启动时,它显示错误强制关闭

public class MyBootRecvr extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        Intent i = new Intent(context, Home.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        i.addFlags(Intent.FLAG_FROM_BACKGROUND);
        context.startActivity(i);
        Toast.makeText(context, "Where is my KeyBoard", Toast.LENGTH_LONG)
                .show();
    }
}

应用程序中的权限和接收者标记。


<receiver
     android:name=".Home"
     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>

2 个答案:

答案 0 :(得分:0)

如果这是您应用程序中唯一的代码,它将永远不会运行。 Android应用程序必须至少有一个Activity的子类作为运行应用程序的起点。活动至少应该是这样的:

class MyActivity extends Activity {
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.my_layout); /* my_layout should be an XML layout in res/layout */
    }
}

确保以下代码是您的AndroidManifest.xml文件

<activity android:name="MyActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

如果您在Eclipse中创建一个新的Android项目,它将为您设置此项。关于在Android中设置基本应用程序以及使用Eclipse为您执行此操作,有很多教程。

答案 1 :(得分:0)

这样做可以解决问题 感谢xono

<receiver
     android:name=".MyBootRecvr"
     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>
相关问题