且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在Android的启动时启动应用程序

更新时间:2022-10-21 12:47:31

最后,寻找了一段时间,对于一些一谁是困在这里后做了正确的事情。你需要做的是注册在一个单独的包中的onReceive函数,我渐渐在logcat中的错误是ClassNotFound的,因为我放在code在我MainActivity类别及其封装。当我没有改变的onReceive功能,新的软件包应用完美地工作。

I am trying to launch the main activity (which is the only activity) named as "MainActivity" when the Android OS starts. The app is installed on the internal storage but it says Unfortunately, The app(Name of app) has stopped. I am using the Boot Receive code as below

My MainActivity code in .java file

public class BootUpReceiver extends BroadcastReceiver
{

    @Override
    public void onReceive(Context context, Intent intent) {
            Intent i = new Intent(context, MainActivity.class);  
            i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(i);  
    }
}

AndroidManifest.xml

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

      <receiver android:enabled="true" android:name=".BootUpReceiver"
    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>

Finally, did the right thing after searching some time, For some one who is stuck up here. What you have to do is register the onReceive function in a separate package, what i was getting the error in Logcat was Classnotfound as i placed the code in my MainActivity class and its package. As soon as i did changed the onReceive function to new package the application worked flawlessly.