【3D技术宅公社】XR数字艺术论坛  XR技术讨论 XR互动电影 定格动画

 找回密码
 立即注册

QQ登录

只需一步,快速开始

调查问卷
论坛即将给大家带来全新的技术服务,面向三围图形学、游戏、动画的全新服务论坛升级为UTF8版本后,中文用户名和用户密码中有中文的都无法登陆,请发邮件到324007255(at)QQ.com联系手动修改密码

3D技术论坛将以计算机图形学为核心,面向教育 推出国内的三维教育引擎该项目在持续研发当中,感谢大家的关注。

查看: 1349|回复: 0

android activity/service开机后自动运行

[复制链接]
发表于 2013-1-15 22:50:35 | 显示全部楼层 |阅读模式
看了网上的几个例子,也做了一个系统启动后直接运行activity的小程序
代码贴在下面:
首先是从BroadcastReceiver派生出一个新类,用来监听系统启动后发出的广播消息android.intent.action.BOOT_COMPLETED。
BootReceiver.java:
  1. import android.content.BroadcastReceiver;
  2. import android.content.Context;
  3. import android.content.Intent;
  4. import android.util.Log;

  5. public class BootReceiver extends BroadcastReceiver {

  6.     public void onReceive(Context context, Intent intent) {
  7.          

  8.          if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
  9.          {
  10.               Log.d("BootReceiver", "system boot completed");
  11.               Intent newIntent = new Intent(context, FirstRun.class);
  12.          newIntent.setAction("android.intent.action.MAIN"); //MyActivity action defined in AndroidManifest.xml

  13.          newIntent.addCategory("android.intent.category.LAUNCHER");//MyActivity category defined in AndroidManifest.xml

  14.          newIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //If activity is not launched in Activity environment, this flag is mandatory to set

  15.          context.startActivity(newIntent);

  16.          //if you want to start a service, follow below method:


  17.          /*******************************************************

  18.             Intent service = new Intent(yourService.ACTION_START);
  19.             service.setClass(context, yourService.class);
  20.             context.startService(service);


  21.          ******************************************************/
  22.          }
  23.      }
  24. }
复制代码
接下来这个类就是监听到系统启动完毕后,我们要运行的activity.
FirstRun.java
  1. import android.app.Activity;
  2. import android.os.Bundle;

  3. public class FirstRun extends Activity {
  4.    
  5.     public void onCreate(Bundle savedInstanceState) {
  6.         super.onCreate(savedInstanceState);
  7.         setContentView(R.layout.main);
  8.     }
  9. }
复制代码
当然,我们还要改配置文件,需要注意的是,在manifest.xml中需要加上
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>

Manifest.xml
  1. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  2.       package="com.service.prac"
  3.       android:versionCode="1"
  4.       android:versionName="1.0">
  5.       
  6.     <application android:icon="@drawable/icon" android:label="@string/app_name">
  7.         <receiver android:name=".BootReceiver"
  8.                   android:label="@string/app_name">
  9.             <intent-filter>
  10.                 <action android:name="android.intent.action.BOOT_COMPLETED"/>
  11.                 <category android:name="android.intent.category.LAUNCHER" />
  12.             </intent-filter>
  13.         </receiver>
  14.         <activity android:name=".FirstRun">
  15.             <intent-filter>
  16.                 <action android:name="android.intent.action.MAIN" />
  17.                 <category android:name="android.intent.category.LAUNCHER" />
  18.             </intent-filter>
  19.         </activity>
  20.     </application>
  21.     <uses-sdk android:minSdkVersion="3" />
  22.     <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"></uses-permission>
  23. </manifest>
复制代码

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|3D数字艺术论坛 ( 沪ICP备14023054号 )

GMT+8, 2024-6-29 05:59

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表