2017-06-29 18 views
5

Kullanıcıya internet bağlantısını kaybettiğinde bilmek istediğim gibi bir süre sonra bir şeyler yapmak için Android'de Sheduled Task özelliğini eklemeyi deniyorum, sonra bir uyarı iletişim kutusu açmak istiyorum. Bu yüzden Sheduled Task Execution kullanarak yapıyorum ama ne zaman Runnable, benim çalışma kodu koydum, Görev işe yaramadı. ÖnemliNeden Görev Yürütme sabit hızı programındaki kod çalışmıyor?

Hizmetin sınıfında yapıyorum edilir

KOD ben sheduled görevde bir şey yapıyor ve tek satır baskı yoktu

package com.example.sid.marwadishaadi.LoginHistory; 

import android.app.Service; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.content.SharedPreferences; 
import android.net.ConnectivityManager; 
import android.net.NetworkInfo; 
import android.os.AsyncTask; 
import android.os.IBinder; 
import android.provider.Settings; 
    import android.support.v7.app.AlertDialog; 
    import android.util.Log; 
    import android.widget.Toast; 

    import java.util.concurrent.Executors; 

    import java.util.concurrent.ScheduledExecutorService; 
    import java.util.concurrent.TimeUnit; 

    import static com.bumptech.glide.gifdecoder.GifHeaderParser.TAG; 

     public class OnClearFromRecentService extends Service { 

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

      @Override 
      public int onStartCommand(Intent intent, int flags, int startId) { 
       Log.e("ClearFromRecentService-", "-----------------------------------------Service Started"); 
       SharedPreferences sharedPreferences=getSharedPreferences("userinfo",MODE_PRIVATE); 
       SharedPreferences.Editor edtr=sharedPreferences.edit(); 
       String id=sharedPreferences.getString("customer_id",""); 
       Log.e(TAG, "onStartCommand: .........................."+id); 
       if(isOnline()) { 
        Toast.makeText(this, "You are online", Toast.LENGTH_SHORT).show(); 
       } 

       ScheduledExecutorService scheduleTaskExecutor = Executors.newScheduledThreadPool(5); 
       scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() { 
        public void run() {        

         Log.i(TAG, "run: ----I'm running after 15 seconds"); 
         if(isOnline()) { 
          //Dummy TODO, you can do something if you want, 
          Toast.makeText(OnClearFromRecentService.this, "You are not online", Toast.LENGTH_SHORT).show(); 
         } 

         else{ 
          Log.i(TAG, "run: --- exited from here or not :::: yes "); 
          AlertDialog.Builder network =new AlertDialog.Builder(OnClearFromRecentService.this); 
          network.setTitle("No Internet"); 
          network.setMessage("Please check your internet connection or go to the internet option by clicking #settings"); 
          network.setPositiveButton("Settings", new DialogInterface.OnClickListener() { 
           @Override 
           public void onClick(DialogInterface dialogInterface, int i) { 
            getApplicationContext().startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS)); 
           } 
          }); 
          network.setNegativeButton("Exit", new DialogInterface.OnClickListener() { 
           @Override 
           public void onClick(DialogInterface dialogInterface, int i) { 
            Intent intent = new Intent(Intent.ACTION_MAIN); 
            intent.addCategory(Intent.CATEGORY_HOME); 
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 
            startActivity(intent); 
           } 
          }); 
          network.setCancelable(false); 
          AlertDialog alertDialog = network.create(); 
          alertDialog.show(); 


         } 

        } 
       }, 0, 15, TimeUnit.SECONDS); 

       return START_NOT_STICKY; 
      } 

     public boolean isOnline() { 
    ConnectivityManager conMgr = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo netInfo = conMgr.getActiveNetworkInfo(); 

    if(netInfo == null || !netInfo.isConnected() || !netInfo.isAvailable()){ 
     Toast.makeText(getApplicationContext(), "No Internet connection!", Toast.LENGTH_LONG).show(); 
     return false; 
    } 
    return true; 
} 



      @Override 
      public void onDestroy() { 
       super.onDestroy(); 
       Log.e("ClearFromRecentService-", "-----------------------------------Service Destroyed"); 
      } 

      @Override 
      public void onTaskRemoved(Intent rootIntent) { 
       Log.e("Clearvi--------------", "---------------------------------END"); 
       //Code here 

       stopSelf(); 
      } 


     } 

sonra

gibi iyi çalışır IS

log.e ("", "15 saniye sonra koşuyorum") - >> günlük hat yazdır

ancak kodumu koyduğumda, kod çalışmadığı gibi işe yaramaz.

Herkes bir şey önerebilir, noob için gerçekten yararlı olacaktır.

+0

herhangi bir hata günlüğü? – Kaushal28

+0

@ Kaushal28 Hata bulunamadı –

+0

kodunuzu "try catch" ile silmeyi deneyin ve hataları kontrol edin. Aksi takdirde iyi çalışması gerekir. – Kaushal28

cevap

2

try-catch satırında run yöntemini kaydırın.

Sadece bir tahmin: Bir istisna atılıyor. Bir Özel Durumla karşılaşırsa, ScheduledExecutorService sessizce durur.

Çalıştırma yönteminin kodu, atılan Özel Durumları işlemek ve emmek için her zaman bir try-catch ile çevrelenmelidir.

Yakalama işlemini denemeden önce bir Looper yapmaya çalışırsanız ve bunu açık hale getirmeye çalışırsanız, iyi çalışır çünkü bir iş parçacığından bir UI iş parçacığını işleyemezsiniz.

+0

Evet, işe yaradı ama her defasında istisna atar. Hata ** Beklenmedik istisna, lüper bir görünüm etkinlik kısmı ele alamıyor ** –

+0

Teşekkürler, hata yakalama ve yakalama denemeden önce bir lüper yapmak ve çok iyi çalıştı, Teşekkürler + 1 –

İlgili konular