手机
当前位置:查字典教程网 >编程开发 >安卓软件开发 >android基础教程之开机启动示例
android基础教程之开机启动示例
摘要:Manifest.xml文件:复制代码代码如下:注释:因为我们这个服务要一直在后台运行,所以不采用bindService的方式,而是直接采用...

Manifest.xml文件:

复制代码 代码如下:

<service

android:name=".DaemonService"

android:enabled="true"

android:process=".DaemonService" >

<intent-filter android:priority="1000">

<action android:name="cn.test.DaemonService" />

<category android:name="android.intent.category.DEFAULT" />

</intent-filter>

</service>

注释:因为我们这个服务要一直在后台运行,所以不采用bindService的方式,而是直接采用startService的方式。

这样就不至于我们的程序结束,也把我们的服务也结束掉了。

复制代码 代码如下:

package cn.start.test;

import java.util.List;

import android.annotation.SuppressLint;

import android.app.ActivityManager;

import android.app.Service;

import android.app.ActivityManager.RunningAppProcessInfo;

import android.content.Intent;

import android.os.Handler;

import android.os.IBinder;

import android.util.Log;

public class DaemonService extends Service {

private static final String TAG = "Alarmreceiver";

Handler hd1 = new Handler();

int delay = 5000;

public IBinder onBind(Intent intent) {

// TODO Auto-generated method stub

return null;

}

@SuppressLint("NewApi")

public void onCreate() {

System.out.println("服务启动成功。。。。。。。。。。");

hd1.postDelayed(mTasks, delay);

}

private Runnable mTasks = new Runnable() {

@SuppressLint("NewApi")

public void run() {

android.os.Process.setThreadPriority(android.os.Process.THREAD_PRIORITY_FOREGROUND);

if (checkMainAppIsActive()) {

Log.d(TAG, "服务检测主应用还在运行");

} else {

Log.d(TAG, "服务检测主应用已经关闭");

Intent intent = getPackageManager().getLaunchIntentForPackage(

"cn.start.test");

if (intent != null) {

DaemonService.this.startActivity(intent);

Log.d(TAG, "服务启动主应用程序。");

} else {

Log.d(TAG, "服务检测到没有安装主应用,自动退出。");

return;

}

}

hd1.postDelayed(mTasks, delay);

}

};

/**

* 检测是否主程序是否还在运行

* @return

*/

@SuppressLint({ "NewApi", "NewApi" })

public boolean checkMainAppIsActive(){

ActivityManager am = (ActivityManager) getSystemService(ACTIVITY_SERVICE);

//获取正在运行的应用

List<RunningAppProcessInfo> run = am.getRunningAppProcesses();

for(RunningAppProcessInfo ra : run){

if(ra.processName.equals("cn.start.test")){

return true;

}

}

return false;

}

}

开机自动启动程序,自然少不了开机启动广播了。

manifest.xml 文件:

复制代码 代码如下:

<receiver android:name=".StartupReceiver" >

<intent-filter android:priority="1000" >

<action android:name="android.intent.action.BOOT_COMPLETED" />

</intent-filter>

</receiver>

复制代码 代码如下:

public class StartupReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

// TODO Auto-generated method stub

Intent i = new Intent(context,LoginActivity.class);

i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

context.startActivity(i);

Intent serviceIntent = new Intent(context,DaemonService.class);

context.startService(serviceIntent);

}

}

总结:开机启动广播一定在manifest文件中注册好。定时器要注意定时延迟。

通过List<RunningAppProcessInfo> run = am.getRunningAppProcesses();判断自己的进程是否还在运行

通过getPackageManager().getLaunchIntentForPackage( "cn.start.test"); getPackageManager().getLaunchIntentForPackage( "cn.start.test");

来启动自己的程序。

【android基础教程之开机启动示例】相关文章:

Android开源组件小结

android滑动解锁震动效果的开启和取消

Android开发之选项组件

android 调用系统的照相机和图库实例详解

android 多线程技术应用

Android开场动画实例类Java代码

Android:多线程之进程与线程

android IPC之binder通信机制

android 开发教程之日历项目实践(三)

Android里实现退出主程序的提示代码

精品推荐
分类导航