gpt4 book ai didi

java - 服务类别中的持续通知导致手机死机

转载 作者:行者123 更新时间:2023-12-01 12:34:12 26 4
gpt4 key购买 nike

目前,我有一个服务类,可以在选定的时间内锁定应用程序。我正在尝试在正在进行的通知中显示用户的剩余时间。

但是,问题是,当我在手机上运行它时,它变得非常滞后,并且整个通知栏都被清空了。

我已经做了研究,但到目前为止还没有运气。

Android: How to create an "Ongoing" notification?

Notification at end of CountDownTimer

并尝试使用未弃用的通知方法;但是,当我使用通知生成器时,我的手机就会断断续续地卡住。没有日志报告。我不知道该怎么办。

这是我的服务类别:

package com.ibc.android.demo.appslist.app;

import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class HeartBeat extends Service {

ArrayList<String> packagezList;
SharedPreferences sharedPrefs;
Map<String, ?> allEntries;
SharedPreferences sharedPrefsapp;
SharedPreferences endTimerPreferences;
long timerends;

@Override
public IBinder onBind(Intent arg0) {
return null;
}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {

//startService(new Intent(this, HeartBeat.class));

endTimerPreferences = getApplicationContext().getSharedPreferences("endservice", Context.MODE_PRIVATE);
timerends= endTimerPreferences.getLong("endservice", 0);

//Log.e("MONOLO ", timerends + "");

sharedPrefs = getApplicationContext().getSharedPreferences(getApplicationContext().getPackageName(), Context.MODE_PRIVATE);
sharedPrefsapp = getApplicationContext().getSharedPreferences("appdb", Context.MODE_PRIVATE);
allEntries= null;
allEntries = sharedPrefsapp.getAll();
packagezList= null;

packagezList = new ArrayList<String>();

for (Map.Entry<String, ?> entry : allEntries.entrySet()) {
//Log.e("right key: ", entry.getKey() + "right value: " + entry.getValue().toString() );
packagezList.add(entry.getKey());

}

/*for(Object object: packagezList){
Log.e("YO!", (String) object);
}*/

ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);

try {
//List<RecentTaskInfo> recentTasks = activityManager.getRecentTasks(1, ActivityManager.RECENT_IGNORE_UNAVAILABLE);
ActivityManager mActivityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> RunningTask = mActivityManager
.getRunningTasks(1);
ActivityManager.RunningTaskInfo ar = RunningTask.get(0);
String activityOnTop = ar.topActivity.getPackageName();

// Log.e("activity on Top", "" + activityOnTop);
// Log.e(" My package name", "" + getApplicationContext().getPackageName());

//for (Object data : newArrayList) {

if(System.currentTimeMillis() < timerends ) {

long second = (timerends / 1000) % 60;
long minute = (timerends / (1000 * 60)) % 60;
long hour = (timerends / (1000 * 60 * 60)) % 24;

String time = String.format("%02d:%02d:%02d", hour, minute, second);

/* NotificationManager notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
Notification myNotification = new Notification(R.drawable.ic_launcher, "Apps now locked!", System.currentTimeMillis());
Context context = getApplicationContext();
String notificationTitle = "Apps blocked!";
String notificationText = "Time Remaining: " + time;
Intent myIntent = new Intent(HeartBeat.this, HeartBeat.class);
PendingIntent pendingIntent = PendingIntent.getActivity(HeartBeat.this, 0, myIntent, Intent.FILL_IN_ACTION);
myNotification.flags = Notification.FLAG_ONGOING_EVENT;
myNotification.setLatestEventInfo(context, notificationTitle, notificationText, pendingIntent);
notificationManager.notify(1, myNotification);*/

for (Object object : packagezList) {

// Provide the packagename(s) of apps here, you want to show password activity
if ((activityOnTop.contains((CharSequence) object)) &&
(!activityOnTop.contains(getApplicationContext().getPackageName()
))) { // you have to make this check even better

Intent i = new Intent(getApplicationContext(), LockScreenActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_ANIMATION);
i.putExtra("", "");
startActivity(i);
}

}
}


} catch (Exception e) {
// Log.e("Foreground App", e.getMessage(), e);
}

Intent ishintent = new Intent(this, HeartBeat.class);
PendingIntent pintent = PendingIntent.getService(this, 0, ishintent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pintent);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),5000, pintent);

return START_STICKY;
}

// Log.i("LocalService", "Received start id " + startId + ": " +
// intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.

@Override
public void onDestroy() {

Intent in = new Intent();
in.setAction("YouWillNeverKillMe");
sendBroadcast(in);

Intent ishintent = new Intent(this, HeartBeat.class);
PendingIntent pintent = PendingIntent.getService(this, 0, ishintent, 0);
AlarmManager alarm = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
alarm.cancel(pintent);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),5000, pintent);

//startService(new Intent(this, HeartBeat.class));
}
// this.stopSelf();
//startforeground goes here

}

可能出了什么问题?我确信我的代码是有效的。我不确定如何解决此问题并成功获得剩余时间的持续通知。

最佳答案

本质上,通知会以相同的名称再次重新创建,因此它会占用您的 RAM 并导致您的手机死机。我建议找到一种不同的方法来倒计时通知中的时间。

关于java - 服务类别中的持续通知导致手机死机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25706917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com