在手机启动时启动Android应用程序

时间:2013-03-20 19:31:39

标签: android

我希望我的Android活动能够在手机启动时显示,但应用程序崩溃,我也无法记录日志。这是代码 清单

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

    <uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
         <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>

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

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

</manifest>

BOOTUPRECEIVER.JAVA

package com.example.sample;

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

public class BootUpReceiver extends BroadcastReceiver{


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

         Intent i = new Intent(context, StartupActivity.class); 
                i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(i); 

     }
    }

STARTUPACTIVITY.JAVA

package com.example.sample;

import android.app.Activity;
import android.os.Bundle;

import com.android.R;

public class StartupActivity extends Activity {



     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);


     }
    }

有人可以帮我解决这个问题吗

编辑: 03-20 16:05:24.519:E / AndroidRuntime(2709):java.lang.RuntimeException:无法实例化活动ComponentInfo {com.android/com.android.StartupActivity}:java.lang.ClassNotFoundException:com.android.StartupActivity在loader dalvik.system.PathClassLoader [/data/app/com.android-1.apk]中 03-20 16:05:24.519:E / AndroidRuntime(2709):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573) 03-20 16:05:24.519:E / AndroidRuntime(2709):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 03-20 16:05:24.519:E / AndroidRuntime(2709):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117) 03-20 16:05:24.519:E / AndroidRuntime(2709):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:935) 03-20 16:05:24.519:E / AndroidRuntime(2709):在android.os.Handler.dispatchMessage(Handler.java:99) 03-20 16:05:24.519:E / AndroidRuntime(2709):在android.os.Looper.loop(Looper.java:130) 03-20 16:05:24.519:E / AndroidRuntime(2709):在android.app.ActivityThread.main(ActivityThread.java:3687) 03-20 16:05:24.519:E / AndroidRuntime(2709):at java.lang.reflect.Method.invokeNative(Native Method) 03-20 16:05:24.519:E / AndroidRuntime(2709):at java.lang.reflect.Method.invoke(Method.java:507) 03-20 16:05:24.519:E / AndroidRuntime(2709):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:842) 03-20 16:05:24.519:E / AndroidRuntime(2709):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 03-20 16:05:24.519:E / AndroidRuntime(2709):at dalvik.system.NativeStart.main(Native Method) 03-20 16:05:24.519:E / AndroidRuntime(2709):引起:java.lang.ClassNotFoundException:加载器中的com.android.StartupActivity dalvik.system.PathClassLoader [/data/app/com.android-1.apk ] 03-20 16:05:24.519:E / AndroidRuntime(2709):at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240) 03-20 16:05:24.519:E / AndroidRuntime(2709):at java.lang.ClassLoader.loadClass(ClassLoader.java:551) 03-20 16:05:24.519:E / AndroidRuntime(2709):at java.lang.ClassLoader.loadClass(ClassLoader.java:511) 03-20 16:05:24.519:E / AndroidRuntime(2709):在android.app.Instrumentation.newActivity(Instrumentation.java:1021) 03-20 16:05:24.519:E / AndroidRuntime(2709):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565) 03-20 16:05:24.519:E / AndroidRuntime(2709):... 11 more

1 个答案:

答案 0 :(得分:1)

在Android 3.0及更高版本中,启动接收器不会运行,直到活动至少一次手动启动。所以很可能你没有跑步。

您没有出现在网格中,因为您没有启动器活动。将其添加到清单中的活动代码

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

然后运行应用程序一次。之后它应该工作。

相关问题