SORU
22 Ocak 2011, CUMARTESİ


Android başlamak servisi

Belli bir aktivite başladığında bir hizmet aramak istiyorum. Yani, burada Hizmet sınıfı:

public class UpdaterServiceManager extends Service {

    private final int UPDATE_INTERVAL = 60 * 1000;
    private Timer timer = new Timer();
    private static final int NOTIFICATION_EX = 1;
    private NotificationManager notificationManager;

    public UpdaterServiceManager() {}

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        // Code to execute when the service is first created
    }

    @Override
    public void onDestroy() {
        if (timer != null) {
            timer.cancel();
        }
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startid) {
        notificationManager = (NotificationManager) 
                getSystemService(Context.NOTIFICATION_SERVICE);
        int icon = android.R.drawable.stat_notify_sync;
        CharSequence tickerText = "Hello";
        long when = System.currentTimeMillis();
        Notification notification = new Notification(icon, tickerText, when);
        Context context = getApplicationContext();
        CharSequence contentTitle = "My notification";
        CharSequence contentText = "Hello World!";
        Intent notificationIntent = new Intent(this, Main.class);
        PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
                notificationIntent, 0);
        notification.setLatestEventInfo(context, contentTitle, contentText,
                contentIntent);
        notificationManager.notify(NOTIFICATION_EX, notification);
        Toast.makeText(this, "Started!", Toast.LENGTH_LONG);
        timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                // Check if there are updates here and notify if true
            }
        }, 0, UPDATE_INTERVAL);
        return START_STICKY;
    }

    private void stopService() {
        if (timer != null) timer.cancel();
    }
}

Ve işte onu diyorum nasıl olur:

Intent serviceIntent = new Intent();
serviceIntent.setAction("cidadaos.cidade.data.UpdaterServiceManager");
startService(serviceIntent);

Sorun hiçbir şey olmuyor. Yukarıdaki kod bloğu etkinliğin sonunda onCreate denir. Ben zaten ayıklanacak ve bir istisna atılır.

Herhangi bir fikir?

CEVAP
22 Ocak 2011, CUMARTESİ


Muhtemelen bildiriminde hizmeti yok, ya da eylem eşleşen <intent-filter> yoktur. LogCat (adb logcat, DDMS veya Eclipse DDMS perspektifini) inceleyerek yardımcı olabilecek bazı uyarılar daha da arttırayım.

Daha büyük olasılıkla, hizmeti ile başlamalıdır:

startService(new Intent(this, UpdaterServiceManager.class));

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • jedimasterkyle

    jedimasterky

    11 ŞUBAT 2006
  • oHeymarvin

    oHeymarvin

    11 Temmuz 2013
  • Sams Page :D

    Sams Page :D

    15 Mart 2009