我的应用程序可以跟踪人们在其应用程序上花费的时间,当我在手机上运行时,它可以很好地运行。但是,在生成签名的apk并更改了文件夹和gradle的几个名称后,我成功上传了它。当我再次下载它时,它不起作用! 问题似乎是它只能获取少于10个数据,而且,当我测试它时,数据是不准确的。 我认为问题可能出在目录中,但是我创建了新项目,然后重新输入所有内容仍然存在相同的问题。 这是可能有用的消息:
09-05 15:42:29.618 17735-17735/my.awesome.tony.usagetrack0819 W/System.err: android.content.pm.PackageManager$NameNotFoundException: app.awesome.my.anti_addiciton
at android.app.ApplicationPackageManager.getApplicationInfoAsUser(ApplicationPackageManager.java:506)
at android.app.ApplicationPackageManager.getApplicationInfo(ApplicationPackageManager.java:488)
package my.awesome.tony.usagetrack0819;
这是我的代码:
import android.app.AppOpsManager;
import android.app.usage.UsageStats;
import android.app.usage.UsageStatsManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.icu.util.Calendar;
import android.provider.Settings;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.tony.usagetrack0819.R;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
long begintime;
long endtime;
ListView listView;
Integer TotalForeground = 0;
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
TextView textView;
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
public void getUsageStatsList() throws PackageManager.NameNotFoundException {
// if (ContextCompat.checkSelfPermission(this, Manifest.permission.PACKAGE_USAGE_STATS)
// != PackageManager.PERMISSION_GRANTED) {
// Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
// startActivity(intent);
//
// } else {
Log.i("Chloe", "getUsageStatsList()!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
Calendar c1 = Calendar.getInstance();
c1.set(Calendar.HOUR_OF_DAY, 0);
c1.set(Calendar.MINUTE, 0);
c1.set(Calendar.SECOND, 0);
Calendar c2 = Calendar.getInstance();
begintime = c1.getTimeInMillis();
endtime = c2.getTimeInMillis();
Log.i("Chloe", "usm!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
UsageStatsManager usm = (UsageStatsManager) getSystemService(USAGE_STATS_SERVICE);
assert usm != null;
PackageManager packageManager = getApplicationContext().getPackageManager();
ApplicationInfo ai;
List<UsageStats> usageStatsList = usm.queryUsageStats(UsageStatsManager.INTERVAL_DAILY, begintime, endtime);
ArrayList<String> listViewContent = new ArrayList<String>();
// Log.i("Chloe", "before if(usageStatsList!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
if (usageStatsList != null && !usageStatsList.isEmpty()) {
// Log.i("Chloe", "before for(UsageStats usageStats : usageStatsList)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
for (UsageStats usageStats : usageStatsList) {
// Log.i("Chloe", "Right after for(UsageStats usageStats : usageStatsList)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
long totalTimeInForeground = usageStats.getTotalTimeInForeground();
Integer i = (int) (long) totalTimeInForeground;
i = i / 60000;
if (i != 0) {
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
TotalForeground =TotalForeground + i;
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
String TimeInForegroundString = Integer.toString(i);
ai = packageManager.getApplicationInfo(usageStats.getPackageName(), 0);
String applicationName = (String) (ai != null ? packageManager.getApplicationLabel(ai) : "Unknown");
// Drawable d = getPackageManager().getApplicationIcon(ai);
String content = applicationName + " " + i + " minutes";
listViewContent.add(content);
Log.i("Chloe", content);
Log.i("Chloe", Integer.toString(TotalForeground));
}
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listViewContent);
Log.i("TTTTTT", Integer.toString(TotalForeground));
listView.setAdapter(listAdapter);
textView.setText( "Total time you spent on your phone!! "+ Integer.toString(TotalForeground) +" mins");
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listViewContent);
// listView.setAdapter(listAdapter);
Log.i("Chloe", "The for loop end");
}
Log.i("Chloe", "end of getUsageStatsList");
// }
}
private boolean checkUsagePermission() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
AppOpsManager appOps = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
int mode = 0;
mode = appOps.checkOpNoThrow("android:get_usage_stats", android.os.Process.myUid(), getPackageName());
boolean granted = mode == AppOpsManager.MODE_ALLOWED;
if (!granted) {
AlertDialog dialog = new AlertDialog.Builder(this)
.setMessage("請開啟該程序*使用量資料存取* 並重啟該程序 Please turn on Usage Data Acess in settings and reopen the app ").create();
dialog.show();
Intent intent = new Intent(Settings.ACTION_USAGE_ACCESS_SETTINGS);
startActivityForResult(intent, 1);
return false;
}
}
return true;
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
AppOpsManager appOps = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
int mode = 0;
mode = appOps.checkOpNoThrow("android:get_usage_stats", android.os.Process.myUid(), getPackageName());
boolean granted = mode == AppOpsManager.MODE_ALLOWED;
if (!granted) {
Toast.makeText(this, "请开启该权限", Toast.LENGTH_SHORT).show();
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
textView = findViewById(R.id.textView);
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
// if(ContextCompat.checkSelfPermission(this, Manifest.permission.PACKAGE_USAGE_STATS)!= PackageManager.PERMISSION_GRANTED)
// {
// Intent intent = new Intent (Settings.ACTION_USAGE_ACCESS_SETTINGS);
// startActivity(intent);
// }
Log.i("Tony","ON CREATEEEEEE!!!!!");
listView = findViewById(R.id.listView);
try {
getUsageStatsList();
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
checkUsagePermission();
}
}
顺便说一句,该应用程序的名称为Easy Tracker,您可以通过我的名字“ tonymimi”进行搜索,它将更加轻松。