目标:创建一个应用程序,用于获取用户手机上的所有应用程序并将其显示在列表视图中。用户可以在该列表上选择应用程序,并为他们自己创建“时间限制”,以确定他们通过计时器在当天使用该应用程序的时间。该应用程序将在后台运行服务,如果用户正在使用所述应用程序,则每秒检查一次,如果是,则计时器将倒计时,否则,计时器将“暂停”。当计时器达到0时,将出现弹出窗口,通知用户他们已达到当天的时间限制。关闭弹出窗口后,用户将从应用程序中启动。
问题:我不确定我的方法是否用于跟踪应用程序使用情况并且每秒都会调用计时器。我尝试在这里和Android的API上搜索,但我在理解如何让我的服务不断调用该方法时遇到了一些麻烦。以下是我的服务。
我很乐意提供我的代码中可能有助于解决我的困境的任何其他必要部分。在此先感谢您的帮助!
DataTrack.java
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.os.CountDownTimer;
import android.os.IBinder;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.support.annotation.Nullable;
import android.net.TrafficStats;
import android.widget.Toast;
import java.util.List;
import java.util.Timer;
public class DataTrack extends Service {
int idNum = MainActivity.id;
public CountDownTimer timer;
int minPerHour = 60;
int secPerMin = 60;
int milliPerSec = 1000;
long timeInMilliSec;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
//On creation of the service, it creates a timer using the hours and minutes the user set.
//After the initial creation, all timer functionality is handled in the "track" method below.
@Override
public void onCreate(){
MainActivity.hasTimer = true;
timer = new CountDownTimer((MainActivity.hours * minPerHour * secPerMin * milliPerSec) + (MainActivity.minutes * minPerHour * secPerMin * milliPerSec), 100) {
int resetHour = R.id.editHours;
int resetMin = R.id.editMinutes;
@Override
public void onTick(long millisUntilFinished) {
timeInMilliSec = millisUntilFinished;
}
@Override
public void onFinish() {
AlertDialog.Builder builder1 = new AlertDialog.Builder(getApplicationContext())
.setTitle("Times Up!")
.setMessage("You've run out of time, come back tomorrow!")
.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
});
final AlertDialog timesUp = builder1.create();
timesUp.show();
}
};
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
track();
return Service.START_NOT_STICKY;
}
//Tracks app usage by getting the app name and comparing it to a list of names of apps currently running.
// If the app is running, it creates a new timer. In the onTick, it stores
//the remaining time in the "timeInMilliSec" variable. If the app is not running in the foreground, or is executed
//the timer is deleted. Once the app is started up again, the timer is re-created using remaining time
//in the "timeInMilliSec" variable. This is my way of "pausing" and "resuming" the timer count down.
public void track(){
String appName = MainActivity.applName;
ActivityManager activityManager = (ActivityManager) this.getSystemService( ACTIVITY_SERVICE );
List<ActivityManager.RunningAppProcessInfo> procInfos = activityManager.getRunningAppProcesses();
for(int i = 0; i < procInfos.size(); i++)
{
if(procInfos.get(i).processName.equals(appName))
{
timer = new CountDownTimer(timeInMilliSec, 100) {
@Override
public void onTick(long millisUntilFinished) {
timeInMilliSec = millisUntilFinished;
}
@Override
public void onFinish() {
AlertDialog.Builder builder1 = new AlertDialog.Builder(getApplicationContext())
.setTitle("Times Up!")
.setMessage("You've run out of time, come back tomorrow!")
.setPositiveButton("Ok", new DialogInterface.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
});
final AlertDialog timesUp = builder1.create();
timesUp.show();
}
};
}
else{
if(timer != null){
timer.cancel();
timer = null;
}
}
}
}
@Override
public void onDestroy(){
}
}