请选择 进入手机版 | 继续访问电脑版
查看: 483|回复: 0

[Java语言] 开机自启动

3万

主题

3万

帖子

10万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
100197
发表于 2016-8-13 15:26:00
关于让程序在手机开机的时候自启动,给一个小的Demo
  1. package com.android.antking.startloading;
  2. import android.app.Activity;
  3. import android.content.BroadcastReceiver;
  4. import android.content.Context;
  5. import android.content.Intent;
  6. import android.os.Bundle;
  7. public class StartActivity extends Activity {
  8. /** Called when the activity is first created. */
  9. @Override
  10. public void onCreate(Bundle savedInstanceState) {
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.main);
  13. }
  14. }
复制代码
  1. package com.android.antking.startloading;
  2. import android.content.BroadcastReceiver;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. public class StartupReceiver extends BroadcastReceiver{
  6. @Override
  7. public void onReceive(Context context, Intent intent) {
  8. // TODO Auto-generated method stub
  9. Intent i = new Intent(context,StartActivity.class);
  10. i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
  11. context.startActivity(i);
  12. }
  13. }
复制代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="com.android.antking.startloading"
  4. android:versionCode="1"
  5. android:versionName="1.0">
  6. <uses-sdk android:minSdkVersion="7" />
  7. <application android:icon="@drawable/icon" android:label="@string/app_name">
  8. <activity android:name=".StartActivity"
  9. android:label="@string/app_name">
  10. <intent-filter>
  11. <action android:name="android.intent.action.MAIN" />
  12. <category android:name="android.intent.category.LAUNCHER" />
  13. </intent-filter>
  14. </activity>
  15. <receiver android:name=".StartupReceiver">
  16. <intent-filter>
  17. <action android:name="android.intent.action.BOOT_COMPLETED" />
  18. <category android:name="android.intent.category.HOME" />
  19. </intent-filter>
  20. </receiver>
  21. </application>
  22. </manifest>
复制代码
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="fill_parent"
  5. android:layout_height="fill_parent"
  6. >
  7. <TextView
  8. android:layout_width="fill_parent"
  9. android:layout_height="wrap_content"
  10. android:text="@string/hello"
  11. />
  12. <TextView
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:text="开机启动"
  16. ></TextView>
  17. </LinearLayout>
复制代码


回复

使用道具 举报