2013-04-04 138 views
0

我使用AlarmManager啓動服務,但出於某種原因,我無法弄清楚服務未啓動。爲什麼我的服務不開始?

在我的onResume()的代碼用於啓動該服務是:

Intent appIntent = new Intent(cxt, NotificationService.class); 

      AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 
      PendingIntent penIntent = PendingIntent.getService(this, 0, 
        appIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

      alarmManager.cancel(penIntent); 

      penIntent = PendingIntent.getService(this, 0, appIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

      Calendar cal = Calendar.getInstance(); 
      cal.set(Calendar.HOUR_OF_DAY, 5); 
      cal.set(Calendar.MINUTE, 00); 
      cal.set(Calendar.SECOND, 00); 

      alarmManager.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, cal.getTimeInMillis(), 
        10 * 1000, penIntent); 

清單中包含以下內容:

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" /> 
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" /> 
<uses-permission android:name="android.permission.WAKE_LOCK" /> 

<application 
    android:debuggable="true" 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" > 
     <service 
      android:name="de.brillow.mensa.Speiseplan$NotificationService" 
      android:enabled="true" /> 

最後是服務本身是這樣的:

public class NotificationService extends Service { 

     private WakeLock mWakeLock; 

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

     private void handleIntent(Intent intent) { 
      // obtain the wake lock 
      PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE); 
      mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, 
        "wakelocktag"); 
      mWakeLock.acquire(); 

      Log.i("PowerManager", "acquired"); 

      // do the actual work, in a separate thread 
      new PollTask().execute(); 
     } 

     private class PollTask extends AsyncTask<Void, Void, Void> { 

      @Override 
      protected Void doInBackground(Void... params) { 
       // do stuff! 
       Log.i("Background", "stuff"); 
       return null; 
      } 


      @Override 
      protected void onPostExecute(Void result) { 
       // handle your data 
       int id = 001; 
       Log.i("Noti", "fication"); 
       NotificationManager mNotifyMng = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
       NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
         Speiseplan.this).setSmallIcon(R.drawable.icon) 
         .setContentTitle("Test").setContentText("Test!"); 

       mNotifyMng.notify(id, mBuilder.build()); 
       Log.i("Notification", "sent"); 
       stopSelf(); 
       Log.i("Service", "stopped"); 
      } 
     } 


     @Override 
     public void onStart(Intent intent, int startId) { 
      handleIntent(intent); 
     } 


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


     public void onDestroy() { 
      super.onDestroy(); 
      mWakeLock.release(); 
     } 
    } 

由於某種原因,通知和Log.i消息都不出現。我希望你能幫忙。

在此先感謝!

回答

1

您不應在Service中使用AsyncTaskAsyncTask只應用於將結果返回到當前活動中的任務,即它應該在ActivityFragment中定義。這就是它的設計目的。

對於您的情況,您可以將doInBackground(...)onPostExecute(...)之間的所有內容放入您的服務的onCreate(...)。如果你想使用線程。如果您需要在Activity中顯示服務結果,請調用Intent並將任何詳細信息轉換爲附加內容,例如,

調用服務...

Intent intent=new Intent(context,VisualizingActivity.class); 
intent.putExtra("A", "Data A"); 
intent.putExtra("B", "Data B"); 
context.startActivity(intent); 

處理的活動......

String A= intent.getStringExtra("A"); 
String B= intent.getStringExtra("B"); 

如果你只是想創建Notification是你可以從服務直接做到。不需要AsyncTaskActivity

希望這會有所幫助...乾杯!

+0

它有一點幫助,謝謝。但它不能解決我的問題。該服務(儘可能嚴格編碼)dooesn甚至沒有開始... – TheBrillowable 2013-04-04 19:56:49

+1

1.刪除'AsyncTask'; 2.在Manifest中聲明服務:'android:name =「de.brillow.mensa.NotificationService」'; 3.從某處開始你的服務:'Intent service = new Intent(context,NotificationService.class); context.startService(service);' – Trinimon 2013-04-04 20:02:24

+1

這是我犯的錯誤。我把服務放入我的主要活動.java文件中。把它放在一個新的文件中,並進行你提到的調整,現在開始,非常感謝! – TheBrillowable 2013-04-04 20:11:55

相關問題