我的后台服务的哪一部分不正确?

时间:2019-02-25 21:14:34

标签: android service timertask background-service

我有始终运行的后台服务,并定期发送截击请求。

我很确定我的截击请求是正确的,因为我在应用程序的其他部分使用了此命令,并且它可以正常工作。

我想知道这段代码的哪一部分使我的应用程序崩溃。

对不起我的英语不好!

public class CheckMessagesService extends Service {

    public CheckMessagesService() {}

    @Override
    public void onCreate() {
        super.onCreate();

        timer = new Timer();
        timer.schedule(timerTask, 10 * 1000, 10 * 60 * 1000);
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }

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

    @Override
    public void onTaskRemoved(Intent rootIntent) {
        Intent restartServiceIntent = new Intent(getApplicationContext(), this.getClass());
        restartServiceIntent.setPackage(getPackageName());
        startService(restartServiceIntent);

        super.onTaskRemoved(rootIntent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        try {
            timer.cancel();
            timerTask.cancel();
        } catch (Exception ignore) {}
    }

    private Timer timer;

    TimerTask timerTask = new TimerTask() {
        @Override
        public void run() {
            Response.Listener<String> responseListener = new Response.Listener<String>(){
                @Override
                public void onResponse(String response) {
                    if (!response.equals("alright")) {
                        try {
                            JSONArray messagesArray = new JSONArray(response);
                            JSONObject message;

                            message = messagesArray.getJSONObject(0);

                            String id = message.getString("id");
                            String title = message.getString("title");
                            String content = message.getString("content");

                            Intent intent = new Intent(getApplicationContext(), MainActivity.class);
                            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                            PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0);

                            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(getApplicationContext(), "1")
                                    .setSmallIcon(R.drawable.notification_pizza_24dp)
                                    .setContentTitle(title)
                                    .setContentText(content)
                                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                                    .setContentIntent(pendingIntent)
                                    .setAutoCancel(true);

                            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                                CharSequence name = "pizza";
                                String description = "pizza messages";
                                int importance = NotificationManager.IMPORTANCE_DEFAULT;
                                NotificationChannel channel = new NotificationChannel("1", name, importance);
                                channel.setDescription(description);
                                NotificationManager notificationManager = getApplicationContext().getSystemService(NotificationManager.class);
                                notificationManager.createNotificationChannel(channel);
                            }

                            NotificationManagerCompat notificationManager = NotificationManagerCompat.from(getApplicationContext());
                            notificationManager.notify(1, mBuilder.build());

                            SharedPreferences lastReadMessageSharedPreferences = getSharedPreferences("last read message",MODE_PRIVATE);
                            SharedPreferences.Editor editor = lastReadMessageSharedPreferences.edit();
                            editor.putString("notified id" , id);
                            editor.apply();
                        } catch (JSONException ignore) {}
                    }
                }
            };

            SharedPreferences signInDataSharedPreferences = getApplicationContext().getSharedPreferences("sign in data",MODE_PRIVATE);
            String username = signInDataSharedPreferences.getString("username", "");

            SharedPreferences lastReadMessageSharedPreferences = getApplicationContext().getSharedPreferences("last read message",MODE_PRIVATE);
            String id = lastReadMessageSharedPreferences.getString("notified id", "0");

            MessagesRequest messagesRequest = new MessagesRequest(username, id, responseListener);
            messagesRequest.setRetryPolicy(new DefaultRetryPolicy(6000,0,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
            messagesRequest.setShouldCache(false);
            RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
            queue.add(messagesRequest);
        }
    };
}

0 个答案:

没有答案