2011-08-18 83 views
1

我一直在試圖建立一個輪詢系統,每天從服務器獲取數據。所以我的計劃是設置AlarmManager,以便每天12:00開始服務,服務在我的服務器上查找一些信息,並根據服務器的數據在狀態欄中顯示通知。Android,我的服務不會啓動

問題是,我似乎無法以任何方式啓動我的服務。我首先嚐試在AlarmManager中註冊PendingIntent,但我的服務從未開始。現在我只是試圖用startService來啓動它。

那麼我做錯了什麼?

清單:

... 
<application android:icon="@drawable/icon" android:label="@string/app_name"> 
     <activity android:name=".skosexActivity" 
        android:label="@string/app_name" 
        android:theme="@android:style/Theme.NoTitleBar" 
        android:screenOrientation="portrait"> 
      <service android:name=".notificationService"/> 
      <receiver android:name="onBootReceiver" android:enabled="true"> 
       <intent-filter> 
        <action android:name="android.intent.action.BOOT_COMPLETED" /> 
       </intent-filter> 
      </receiver> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 
       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
</application> 
... 

活動:

public class homeActivity extends Activity { 

    static final boolean DEBUG = true; 

    static final String TAG = "HomeActivity: "; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     mContext = getApplicationContext(); 

     //my try on AlarmManager 

     AlarmManager mgr = (AlarmManager)mContext.getSystemService(Context.ALARM_SERVICE); 
     Intent i = new Intent(this, notificationService.class); 
     PendingIntent pi = PendingIntent.getBroadcast(mContext, 0, i, 0); 

     Calendar currentTime = Calendar.getInstance(); 
     currentTime.set(currentTime.get(Calendar.YEAR), 
         currentTime.get(Calendar.MONTH), 
         currentTime.get(Calendar.DAY_OF_MONTH), 14, 44); 

     /*mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 
         SystemClock.elapsedRealtime() + 10000, 
         AlarmManager.INTERVAL_DAY, pi);*/ 

      //my try to just start it some how... 
     Intent intent = new Intent(mContext, notificationService.class); 

     Log.i("HomeActivity", "Trying to service"); 

     startService(new Intent(notificationService.class.getName())); 
    } 
... 

服務代碼:

package se.skosex.pr.skosexp1; 

import android.app.IntentService; 
import android.app.Notification; 
import android.app.NotificationManager; 
import android.app.PendingIntent; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 
import android.widget.Toast; 

public class notificationService extends IntentService { 

    private static final int ID = 1; 

    public notificationService() 
    { 
     super("NotificationService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) 
    { 
     Log.i("NotificationService", "It started!"); 

     String ns = Context.NOTIFICATION_SERVICE; 
     NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); 

     int icon = R.drawable.hagbard; 
     CharSequence tickerText = "Kom och phesta med oss på Skövde sexmästeri kväll!"; 
     long when = System.currentTimeMillis(); 

     Notification notification = new Notification(icon, tickerText, when); 

     Context context = getApplicationContext(); 
     CharSequence contentTitle = "Skövde Sexmästeri, test 2"; 
     CharSequence contentText = "Ikväll är det introdhisko och det betyder at du som nolla inte har några ursäkter att stanna hemma!"; 
     Intent notificationIntent = new Intent(this, eventActivity.class); 
     PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); 

     notification.setLatestEventInfo(this, contentTitle, contentText, contentIntent); 

     mNotificationManager.notify(ID, notification); 
    } 

} 

那麼,我究竟不力錯了嗎?

感謝您的幫助!

+0

我有一個類似的問題服務不啓動並沒有錯誤 http://stackoverflow.com/questions/27113064/start-service-at-specific-time-everyday-by-alarmanager-dont-work – user2964637

回答

0

也許這是你給startService呼叫嘗試這樣的事情,而不是:

Intent intent = new Intent(mContext, notificationService.class); 
    startService(intent) 

它看起來像你的電話給startService()中有一些測試代碼?

+0

不,這不是它。已經嘗試過。謝謝! :) – Mockarutan

0

冒着猜測......也許你錯過了需要的許可?

http://developer.android.com/reference/android/Manifest.permission.html

String SET_ALARM Allows an application to broadcast an Intent to set an alarm for the user. 
+0

嗯,我不想設置鬧鐘,但我的客人,你的意思是使用AlarmManager?這個應用程序,我已經採取了一些代碼:https://github.com/commonsguy/cw-advandroid/tree/master/SystemServices/Alarm。他正在使用AlarmManager,就像我一樣。但我不必爲此申報任何許可。 – Mockarutan

2

註冊服務的Android Manifest.xml文件

+0

這通常是我犯的錯誤... –

0

如果您試圖訪問一個服務器......你需要

<uses-permission android:name="android.permission.INTERNET"/> 

添加到清單或最有可能的意圖將失敗默默