Android 判定APP是否启动、Activity是否运行,通过Service来启动已关闭的Activity

Android 判定APP是否启动、Activity是否运行,通过Service来启动已关闭的Activity,第1张

概述privatestaticfinalStringpackageName="com.example.scangundemo_as";privatestaticfinalStringclassName="MainActivity";1、判定APP是否启动publicstaticbooleanisAppAlive(Contextcontext,StringpackageName){A
    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();    }}

 

总结

以上是内存溢出为你收集整理的Android 判定APP是否启动、Activity是否运行,通过Service来启动已关闭的Activity全部内容,希望文章能够帮你解决Android 判定APP是否启动、Activity是否运行,通过Service来启动已关闭的Activity所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1063597.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存