Good day, the problem:
The application has a Service that checks for new orders on the server.
If the application is minimized, the Service regularly gives out notifications in the blind, but when the application is finished, instead of notifications every minute, a report pops up on the crash of this application.
I can not understand what the problem is.
Listing code attached:
MyDataReceiver:
public class MyDataReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { context.startService(new Intent(context, UpdateService.class)); } } UpdateService:
public class UpdateService extends Service { private Context context; private Timer timer; private UpdateTask timerTask; //public UpdateService(Context _context) {context = _context;} @Override public void onCreate() { if (timer == null) { timer = new Timer(); timerTask = new UpdateTask(getApplicationContext()); timer.schedule(timerTask, 30000, 60000); } } @Override public IBinder onBind(Intent intent) { return null; } } UpdateTask:
public class UpdateTask extends TimerTask { private Context context; public UpdateTask(Context _context) {context = _context;} @Override public void run() { if (WorkSQLite.ReadOrders(context).size() == 0) new OrdersFromDB.CheckNewOrders(context).execute(context.getFilesDir().getPath().toString()); } } NotificationService:
public class NotificationService extends IntentService { NotificationManager nm; @Override public void onCreate() { super.onCreate(); nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); } @Override protected void onHandleIntent(Intent intent) { sendNotif(); } public NotificationService() { super(NotificationService.class.getSimpleName()); } @Override public int onStartCommand(Intent intent, int flags, int startId) { sendNotif(); return super.onStartCommand(intent, flags, startId); } void sendNotif() { Notification myNotification; Intent intent = new Intent(getApplicationContext(), ItemsListActivity.class); PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); Notification.Builder builder = new Notification.Builder(this); builder.setAutoCancel(false); builder.setTicker("Hey. New Orders!"); builder.setContentTitle("Flower Deliver Car"); builder.setContentText("You have a new orders"); builder.setSmallIcon(R.mipmap.ic_launcher); builder.setAutoCancel(true); //builder.setPriority(Notification.PRIORITY_MAX); builder.setDefaults(Notification.DEFAULT_SOUND); builder.setDefaults(Notification.DEFAULT_VIBRATE); builder.setDefaults(Notification.DEFAULT_LIGHTS); //builder.flags |= Notification.FLAG_AUTO_CANCEL; builder.setContentIntent(pIntent); builder.setOngoing(true); myNotification = builder.getNotification(); //myNotification.flags |= Notification.FLAG_FOREGROUND_SERVICE; //startForeground(1, myNotification); nm.notify(1, myNotification); } public IBinder onBind(Intent arg0) { return null; } } UPDATE:
CheckNewOrders:
public static class CheckNewOrders extends AsyncTask<String, Void, Boolean> { private Context context; public CheckNewOrders(Context _context) { this.context = _context; } @Override protected Boolean doInBackground(String... urls) { String track = ""; try {track += "track=" + FileSys.readFile(urls[0]);} catch (Exception ex) {SysEx.ExMessage(ex);} String url_all_products = "http://93.170.141.12:8080/api/app/checkneworders?" + track + "&" + "key=" + KEY + "&" + "cash=" + CashFunc.getCash(KEY); HttpHandler sh = new HttpHandler(); String jsonStr = sh.makeServiceCall(url_all_products); if (jsonStr != null) { try { JSONObject jsonObj = new JSONObject(jsonStr); int success = jsonObj.getInt(TAG_SUCCESS); if (success == 1) return true; } catch (final JSONException e) { Log.e(TAG, "Json parsing error: " + e.getMessage()); } } return false; } @Override protected void onPostExecute(Boolean flag) { //if (flag) super.onPostExecute(flag); //else super.onPostExecute(flag); if (flag) context.startService(new Intent(context, NotificationService.class)); } } StackTrace:
E/AndroidRuntime: FATAL EXCEPTION: Timer-0 java.lang.ExceptionInInitializerError at com.example.zaki.deliverflowerscar.Service.UpdateTask.run(UpdateTask.java:22) at java.util.Timer$TimerImpl.run(Timer.java:284) Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() at android.os.Handler.<init>(Handler.java:121) at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:607) at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:607) at android.os.AsyncTask.<clinit>(AsyncTask.java:190) at com.example.zaki.deliverflowerscar.Service.UpdateTask.run(UpdateTask.java:22) at java.util.Timer$TimerImpl.run(Timer.java:284)