private static final String packageName = "com.example.scangundemo_as";
private static final String className = "MainActivity";
1、判定APP是否启动
public static boolean isAppAlive(Context context, String packageName) {
ActivityManager activityManager =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> processInfos
= activityManager.getRunningAppProcesses();
for (int i = 0; i < processInfos.size(); i++) {
Log.i("Service","--------processname="+processInfos.get(i).processName);
if (processInfos.get(i).processName.equals(packageName)) {
return true;
}
}
return false;
}
2、判定Activity是否运行
public static boolean isRunningForeground (Context context) {
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
String currentPackageName = cn.getPackageName();
if(!TextUtils.isEmpty(currentPackageName) && currentPackageName.equals(context.getPackageName())) {
return true ;
}
return false ;
}
3、通过Service来启动已关闭的Activity
3.1)、service
package com.example.scangundemo_as;
import android.app.ActivityManager;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.res.Resources;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import java.util.List;
import java.util.logging.Logger;
public class APPService extends Service {
private static final String packageName = "com.example.scangundemo_as";
private static final String className = "MainActivity";
@Override
public void onCreate() {
super.onCreate();
Log.i("Service","-------create-------");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.i("Service","-------onDestroy-------");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("Service","-----service onStartCommand...");
new Thread(){
@Override
public void run() {
super.run();
while(true){
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//一直运行
boolean isRunningForeground = isRunningForeground(APPService.this);
// Log.i("Service","-----service isRunningForeground="+isRunningForeground);
if(!isRunningForeground){
Log.i("Service","-----start activity-----");
startActivityTool(APPService.this);
}
}
}
}.start();
return super.onStartCommand(intent, flags, startId);
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public static boolean isAppAlive(Context context, String packageName) {
ActivityManager activityManager =
(ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> processInfos
= activityManager.getRunningAppProcesses();
for (int i = 0; i < processInfos.size(); i++) {
Log.i("Service","--------processname="+processInfos.get(i).processName);
if (processInfos.get(i).processName.equals(packageName)) {
return true;
}
}
return false;
}
public static boolean isRunningForeground (Context context) {
ActivityManager am = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
ComponentName cn = am.getRunningTasks(1).get(0).topActivity;
String currentPackageName = cn.getPackageName();
if(!TextUtils.isEmpty(currentPackageName) && currentPackageName.equals(context.getPackageName())) {
return true ;
}
return false ;
}
public static void startActivityTool(Context context){
Intent intent = new Intent();
intent.setClass(context, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK );
context.startActivity(intent);
}
}
3.2)、清单AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.scangundemo_as">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".APPService" android:enabled="true" android:exported="true">
</service>
<receiver android:name=".BootBroadcastReceiver">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED"></action>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
</manifest>
3.3)、MainActivity
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private static final int RUNNING_INTERVAL = 5000;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//启动service
Intent intent = new Intent(this,APPService.class);
startService(intent);
}
@Override
public void onDestroy(){
super.onDestroy();
}
}