android开发设置应用开机自启动(2)——并使应用实时保持在前台
来源:互联网 发布:家装软件哪个好 编辑:程序博客网 时间:2024/04/29 22:55
对于开机自启动已经在上一篇文章中讲过了;对于使自己的应用实时保持在前台,主要就是开启一个线程实时监听处于前台的应用,如果不是自己的应用则把自己的应用放到前台来;
package com.example.alwaysfront;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;public class BooTBroadcastReceiver extends BroadcastReceiver {@Overridepublic void onReceive(Context context, Intent intent) {// TODO Auto-generated method stubif(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {Intent newIntent = new Intent(context,OneMainActivity.class);newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);context.startActivity(newIntent);}}}
package com.example.alwaysfront;import java.util.List;import android.app.Activity;import android.app.ActivityManager;import android.app.ActivityManager.RunningTaskInfo;import android.content.ComponentName;import android.content.Context;import android.content.Intent;import android.content.pm.PackageInfo;import android.content.pm.PackageManager;import android.content.pm.PackageManager.NameNotFoundException;import android.content.pm.ResolveInfo;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class OneMainActivity extends Activity {private EditText et;private Button bt;private Button bt1;private Handler handler;public static boolean flag = true;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_one_main);et = (EditText) findViewById(R.id.et);bt = (Button) findViewById(R.id.bt);bt1 = (Button) findViewById(R.id.bt1);bt.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {if(UtilPhone.isEmail(et.getText().toString().trim())) {Toast.makeText(OneMainActivity.this, "正确", Toast.LENGTH_SHORT).show();}else {Toast.makeText(OneMainActivity.this, "错误", Toast.LENGTH_SHORT).show();}}});bt1.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View arg0) {flag = false;}});handler = new MyHandler();Thread t = new Mythread();t.start();}class MyHandler extends Handler {@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);if(msg.what == 1) {ActivityManager am = (ActivityManager) OneMainActivity.this.getSystemService(Context.ACTIVITY_SERVICE);@SuppressWarnings("deprecation")List<RunningTaskInfo> tasks = am.getRunningTasks(1);if(!tasks.isEmpty()) {ComponentName topActivity = tasks.get(0).topActivity;if(!topActivity.getPackageName().equals(OneMainActivity.this.getPackageName())) {AppRunningINFront(OneMainActivity.this.getPackageName());}}}}}class Mythread extends Thread {@Overridepublic void run() {super.run();while(flag) {try {Thread.sleep(500);Message msg = handler.obtainMessage();msg.what = 1;handler.sendMessage(msg);} catch (InterruptedException e) {e.printStackTrace();}}}}private void AppRunningINFront(String packageName) { PackageInfo pckageInfo; try { pckageInfo = getPackageManager().getPackageInfo(packageName, 0); Intent resolveIntent = new Intent(Intent.ACTION_MAIN, null); resolveIntent.setPackage(pckageInfo.packageName); PackageManager manager = getPackageManager(); List<ResolveInfo> info = manager.queryIntentActivities(resolveIntent, 0); ResolveInfo ri = info.iterator().next(); if (ri != null) { packageName = ri.activityInfo.packageName; String className = ri.activityInfo.name; Intent intent = new Intent(Intent.ACTION_MAIN); ComponentName cn = new ComponentName(packageName, className); intent.setComponent(cn); startActivity(intent); } } catch (NameNotFoundException e) { e.printStackTrace(); } }}
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.alwaysfront" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> <uses-permission android:name="android.permission.GET_TASKS" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".OneMainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <receiver android:name="com.example.houtaitoqiantai.BooTBroadcastReceiver" > <intent-filter> <action android:name="android.intent.action.BOOT_COMPLETED" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </receiver> </application></manifest>
0 0
- android开发设置应用开机自启动(2)——并使应用实时保持在前台
- android开发设置应用开机自启动
- Android开机自启动应用开发
- android 开机自启动应用 (详细)
- Android 开机自启动应用
- Android 开机自启动应用
- Android 开机自启动应用
- Android 开机自启动应用
- Android 开机自启动应用
- Android开机自启动应用
- Android 开机自启动应用
- Android开机自启动应用
- Android 开机自启动应用
- Android应用开机自启动
- android应用开机自启动&service开机自启动
- 应用开机自启动
- android 开机自启动服务或应用
- Android应用开机自启动失败原因
- matlabFREQZ函数
- ex22.py
- 如果遇到sqlserver2008过期提示该怎么办?
- c语言中sizeof()和strlen()的区别
- 详解 lsusb命令
- android开发设置应用开机自启动(2)——并使应用实时保持在前台
- 浅谈如何将NoSQL引入现有架构系统
- RMQ转换LCA模板 ST算法
- Codeforce 705B 组合/统计 博弈论
- 数据结构实验之二叉树四:还原二叉树
- day8-9(一)
- JS input 限制输入类型
- web特征值判断
- caffe 更改源码,使得输入数据为多个标签