Android判断程序前后台状态
AppStatusService
Java代码
package org.wp.activity;
import java.util.List;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
public class AppStatusService extends Service {
private static final String TAG = "AppStatusService";
private ActivityManager activityManager;
private String packageName;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
activityManager = (ActivityManager) this.getSystemService(Context.ACTIVITY_SERVICE);
packageName = this.getPackageName();
new Thread() {
public void run() {
try {
while (true) {
Thread.sleep(1000);
if (isAppOnForeground()) {
Log.i(TAG, "true");
} else {
Log.i(TAG, "false");
}
}
} catch (Exception e) {
相关新闻>>
- 发表评论
-
- 最新评论 更多>>