0

我需要在设备启动时运行我的应用程序。我尝试了以下代码。这里我的问题是,当我运行我的应用程序并重新启动我的设备意味着它可以工作,如果我关闭我的设备然后再打开意味着自动运行将无法工作。为什么会发生?

MainActivity.java

public class MainActivity extends Activity {

    ImageView mImageView;
    Button swap_btn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mImageView = (ImageView) findViewById(R.id.imageView1);
        swap_btn = (Button) findViewById(R.id.button1);
        mImageView.setImageResource(R.drawable.image_01);
        swap_btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub

                mImageView.setImageResource(R.drawable.image_02);

            }
        });
    }

}

OpenOurAppReceiver.java

public class OpenOurAppReceiver 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);
    }

}

清单.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.bootup_sample_app"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

        <receiver
            android:name=".OpenOurAppReceiver"
            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>
    </application>

</manifest>
4

1 回答 1

0

如果您有一个在设备开机时应该可见的用户界面,请将其设为主屏幕。

于 2012-09-06T14:08:10.803 回答