try {
Runtime rnTm=Runtime.getRuntime();
Process execLang = rnTm.exec(new String[]{"getprop", "persist.sys.language"});
mCurrLocale = new BufferedReader(new InputStreamReader (execLang.getInputStream())).readLine();
execLang.destroy();
Process execCountry = rnTm.exec(new String[]{"getprop", "persist.sys.country"});
mCurrCountry = new BufferedReader(new InputStreamReader (execCountry.getInputStream())).readLine();
execLang.destroy();
Log.e("", "Device locale: "+mCurrLocale+" Co:"+mCurrCountry);
} catch (IOException e) {
e.printStackTrace();
return;
}
catch (SecurityException se) {
se.printStackTrace();
return;
Run Code Online (Sandbox Code Playgroud)
}
It's working fine some phones and tablet.
Run Code Online (Sandbox Code Playgroud)
但有一段时间它导致我的应用程序冻结并在logcat中给出以下结果.
I/System ( 1511): Failed to destroy process 1547
I/System ( 1511): libcore.io.ErrnoException: kill failed: ESRCH (No such process)
I/System ( 1511): …Run Code Online (Sandbox Code Playgroud) 我正在启动一项服务,Alarm Manager每隔 20 秒将GPS数据发送到我的服务器。问题是我的堆和分配的堆大小不断增加。当我分析堆转储时,我发现服务实例的数量等于对startService(). 如何避免这个问题?
公共类 SystemBootListener 扩展 BroadcastReceiver {
// 每30秒重新启动一次服务
私有静态最终长 REPEAT_TIME = 1000 * 10;
@覆盖
公共无效onReceive(上下文上下文,意图意图){
Intent i = new Intent(context, StartLocationServiceAfterReboot.class);
PendingIntent 待处理 = PendingIntent.getBroadcast(context, 0, PendingIntent.FLAG_UPDATE_CURRENT);
// 启动完成后 20 秒启动 - 以便届时所有提供程序都已初始化
日历 cal = Calendar.getInstance();
cal.add(日历.SECOND, 20);
// 每10秒触发一次
// InexactRepeating 允许 Android 优化能耗
AlarmManager 服务 = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
service.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), REPEAT_TIME, 挂起);
}
}
公共类 StartLocationServiceAfterReboot 扩展 BroadcastReceiver {
@覆盖
公共无效onReceive(上下文上下文,意图意图){
if(AppSettings.isRouteConfigured(上下文)){
AppSettings.setServiceRunning(上下文, Boolean.TRUE);
意图服务 = …