Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 服务类中的持续通知导致手机冻结_Java_Android_Android Service_Android Notifications - Fatal编程技术网

Java 服务类中的持续通知导致手机冻结

Java 服务类中的持续通知导致手机冻结,java,android,android-service,android-notifications,Java,Android,Android Service,Android Notifications,目前,我有一个服务类,可以在选定的时间内锁定应用程序。我试图在正在进行的通知中显示用户的剩余时间 然而,问题是,当我在手机上运行它时,它变得非常滞后,整个通知栏都被屏蔽了 我做过研究,但到目前为止运气不好 并尝试对通知使用未弃用的方法;然而,当我使用NotificationBuilder时,我的手机会断断续续。没有日志报告。我不知道该怎么办 这是我的服务课: package com.ibc.android.demo.appslist.app; import android.app.Acti

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

然而,问题是,当我在手机上运行它时,它变得非常滞后,整个通知栏都被屏蔽了

我做过研究,但到目前为止运气不好

并尝试对通知使用未弃用的方法;然而,当我使用NotificationBuilder时,我的手机会断断续续。没有日志报告。我不知道该怎么办

这是我的服务课:

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

    }
package com.ibc.android.demo.appslist.app;
导入android.app.ActivityManager;
导入android.app.AlarmManager;
导入android.app.pendingent;
导入android.app.Service;
导入android.content.Context;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.os.IBinder;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Map;
公共类心跳扩展服务{
ArrayList packagezList;
SharedReferences SharedRefers;
地图等熵;
SharedPreferences SharedPrefersApp;
SharedPreferences endTimerPreferences;
长时间趋势;
@凌驾
公共IBinder onBind(意图arg0){
返回null;
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
//startService(新意图(this,HeartBeat.class));
endTimerReferences=getApplicationContext().getSharedReferences(“endservice”,Context.MODE\u PRIVATE);
timerends=endTimerPreferences.getLong(“endservice”,0);
//Log.e(“MONOLO”,timerends+”);
SharedRefers=getApplicationContext().GetSharedReferences(getApplicationContext().getPackageName(),Context.MODE_PRIVATE);
SharedRefersapp=getApplicationContext().GetSharedReferences(“appdb”,Context.MODE\u PRIVATE);
allEntries=null;
allEntries=sharedPrefsapp.getAll();
packagezList=null;
packagezList=newarraylist();
对于(Map.Entry:allEntries.entrySet()){
//Log.e(“右键:”,entry.getKey()+“右值:”+entry.getValue().toString());
packagezList.add(entry.getKey());
}
/*for(对象:packagezList){
Log.e(“YO!”,(String)对象);
}*/
ActivityManager ActivityManager=(ActivityManager)getApplicationContext().getSystemService(Context.ACTIVITY_服务);
试一试{
//List recentTasks=activityManager.getRecentTasks(1,activityManager.RECENT\u IGNORE\u不可用);
ActivityManager MacActivityManager=(ActivityManager)getSystemService(Context.ACTIVITY_服务);
列表运行任务=mActivityManager
.getRunningTasks(1);
ActivityManager.RunningTaskInfo ar=RunningTask.get(0);
字符串activityOnTop=ar.topActivity.getPackageName();
//Log.e(“活动在顶部”,“活动在顶部”);
//Log.e(“我的包名”,“getApplicationContext().getPackageName());
//用于(对象数据:newArrayList){
如果(System.currentTimeMillis()