opt
This commit is contained in:
@@ -3,6 +3,7 @@ package com.mogo.commons.voice;
|
||||
import android.app.ActivityManager;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.ApplicationInfo;
|
||||
import android.text.TextUtils;
|
||||
|
||||
import com.mogo.utils.logger.Logger;
|
||||
@@ -67,11 +68,15 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
// private constructor
|
||||
mVoiceClient = new VoiceClient( context.getApplicationContext() );
|
||||
mVoiceClient.setCallBack( this );
|
||||
mHasFlush = isVoiceServiceReady( context );
|
||||
initFlushStatus( context );
|
||||
|
||||
Logger.w( TAG, "voice is ready = %s", mHasFlush );
|
||||
}
|
||||
|
||||
private void initFlushStatus( Context context ) {
|
||||
mHasFlush = isVoiceServiceReady( context );
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCmdSelected( String cmd ) {
|
||||
if ( !mCmdMap.containsKey( cmd ) ) {
|
||||
@@ -313,21 +318,79 @@ public class AIAssist implements VoiceClient.VoiceCmdCallBack {
|
||||
}
|
||||
|
||||
private boolean isVoiceServiceReady( Context context ) {
|
||||
if ( isRunningTaskExist( context, "com.zhidao.speech" )
|
||||
&& isRunningTaskExist( context, "com.zhidao.speech.adapter" ) ) {
|
||||
if ( isProcessRunning( context, getPackageUid( context, "com.zhidao.speech" ) )
|
||||
&& isProcessRunning( context, getPackageUid( context, "com.zhidao.speech.adapter" ) ) ) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isRunningTaskExist( Context context, String processName ) {
|
||||
// private boolean isRunningTaskExist( Context context, String processName ) {
|
||||
//// ActivityManager am = ( ActivityManager ) context.getSystemService( Context.ACTIVITY_SERVICE );
|
||||
//// List< ActivityManager.RunningAppProcessInfo > processList = am.getRunningAppProcesses();
|
||||
//// for ( ActivityManager.RunningAppProcessInfo info : processList ) {
|
||||
//// if ( info.processName.equals( processName ) ) {
|
||||
//// return true;
|
||||
//// }
|
||||
//// }
|
||||
//// return false;
|
||||
//// }
|
||||
|
||||
/**
|
||||
* 方法描述:判断某一应用是否正在运行
|
||||
* Created by cafeting on 2017/2/4.
|
||||
*
|
||||
* @param context 上下文
|
||||
* @param packageName 应用的包名
|
||||
* @return true 表示正在运行,false 表示没有运行
|
||||
*/
|
||||
public static boolean isAppRunning( Context context, String packageName ) {
|
||||
ActivityManager am = ( ActivityManager ) context.getSystemService( Context.ACTIVITY_SERVICE );
|
||||
List< ActivityManager.RunningAppProcessInfo > processList = am.getRunningAppProcesses();
|
||||
for ( ActivityManager.RunningAppProcessInfo info : processList ) {
|
||||
if ( info.processName.equals( processName ) ) {
|
||||
List< ActivityManager.RunningTaskInfo > list = am.getRunningTasks( 100 );
|
||||
if ( list.size() <= 0 ) {
|
||||
return false;
|
||||
}
|
||||
for ( ActivityManager.RunningTaskInfo info : list ) {
|
||||
if ( info.baseActivity.getPackageName().equals( packageName ) ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
//获取已安装应用的 uid,-1 表示未安装此应用或程序异常
|
||||
public static int getPackageUid( Context context, String packageName ) {
|
||||
try {
|
||||
ApplicationInfo applicationInfo = context.getPackageManager().getApplicationInfo( packageName, 0 );
|
||||
if ( applicationInfo != null ) {
|
||||
return applicationInfo.uid;
|
||||
}
|
||||
} catch ( Exception e ) {
|
||||
return -1;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断某一 uid 的程序是否有正在运行的进程,即是否存活
|
||||
* Created by cafeting on 2017/2/4.
|
||||
*
|
||||
* @param context 上下文
|
||||
* @param uid 已安装应用的 uid
|
||||
* @return true 表示正在运行,false 表示没有运行
|
||||
*/
|
||||
public static boolean isProcessRunning( Context context, int uid ) {
|
||||
ActivityManager am = ( ActivityManager ) context.getSystemService( Context.ACTIVITY_SERVICE );
|
||||
List< ActivityManager.RunningServiceInfo > runningServiceInfos = am.getRunningServices( 200 );
|
||||
if ( runningServiceInfos.size() > 0 ) {
|
||||
for ( ActivityManager.RunningServiceInfo appProcess : runningServiceInfos ) {
|
||||
if ( uid == appProcess.uid ) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user