2015-05-04 95 views
-3

嘿那裏我試圖每隔15分鐘使用alaram管理器運行android後臺服務,但我無法做到這一點我不會發生什麼錯誤,我不知道什麼是錯的在我的代碼它不工作android報警管理器每15分鐘運行一次後臺服務

嘗試{

 //Create a new PendingIntent and add it to the AlarmManager 
     Intent intent = new Intent(this, RingAlarm.class); 
     PendingIntent pendingIntent = PendingIntent.getActivity(this, 
      12345, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     AlarmManager am = 
      (AlarmManager)getSystemService(Activity.ALARM_SERVICE); 
     am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 
       3000,pendingIntent); 

     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show(); 

     } 




RingAlaram.class 
public class RingAlarm extends Service { 
     public void onCreate() { 
        Toast.makeText(getApplicationContext(), "hi there", Toast.LENGTH_LONG).show(); 
       } 

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



} 

和最後我的清單

<application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.alarmmanagerexample.AlarmManagerExample" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
      <service 
      android:name=".RingAlarm" 
      android:exported="false" /> 
    </application> 
+1

設置標誌PendingIntent.FLAG_UPDATE_CURRENT –

+0

@ AC-開放源代碼還是它不工作 –

+0

嘗試以下 –

回答

2

您應該使用getService代替getActivity並傳遞應用程序上下文(getApplicationContext)而不是活動上下文(this)。

//Create a new PendingIntent and add it to the AlarmManager 
    Intent intent = new Intent(getApplicationContext(), RingAlarm.class); 
    PendingIntent pendingIntent = PendingIntent.getService(getApplicationContext(), 12345, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
    AlarmManager am = (AlarmManager)getSystemService(Activity.ALARM_SERVICE); 
    am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 3000, pendingIntent); 

RingAlaram.class

public class RingAlarm extends Service { 
    @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     Toast.makeText(getApplicationContext(), "hi there", Toast.LENGTH_LONG).show(); 
    } 

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

還設置標誌PendingIntent.FLAG_UPDATE_CURRENT而不是取消清單 和設置服務聲明<packagename>.RingAlarm可能是com.example.alarmmanagerexample.RingAlarm你的情況(我不知道你的項目結構體)。

+0

這不是正確的答案,但服務代碼不會以這種方式編譯。您必須從onStartCommand()方法返回Service.START_NOT_STICKY或START_STICKY或START_STICKY_COMPATIBILITY。 在這種情況下,Service.START_NOT_STICKY會有意義 –

1

變化getActivity到的getService

Intent intent = new Intent(this, RingAlarm.class); 
     PendingIntent pendingIntent = PendingIntent.getService(this, 
      12345, intent, PendingIntent.FLAG_CANCEL_CURRENT); 
     AlarmManager am = 
      (AlarmManager)getSystemService(Activity.ALARM_SERVICE); 
     am.setRepeating(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime(), 
       3000,pendingIntent); 

     } catch (Exception e) { 
      Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show(); 

     } 
相關問題