2015-10-22 33 views
0

你好,我正在建立一個應用程序,我想添加訂閱。這意味着每個用戶都必須每月付費。不知道服務,BroadcastReceiver和AlarmManager之間的選擇

所以我想檢查用戶是否已經支付他將能夠繼續訂單,如果他沒有,那麼我想要一個對話框重定向他支付。你會建議我使用ServiceBroadcastReceiverAlarmaManager

我正在考慮創建一個Service並在其中創建一個AsyncTask,它將檢查用戶是否已付款,然後如果不通知用戶有對話框。另外我正在考慮爲訂閱結束的用戶創建Notiofications

您的意見是什麼?

回答

1

我開發了一個類似的功能檢查許多賬單。我結合了三種方法來確保穩定性。但我認爲您應該使用Google Play應用內結算來實現訂閱,而不是使用本地數據庫。如果您必須使用數據庫來訂閱:

1.用戶訂閱後,將保存的信息保存到數據庫並啓動服務。服務啓動一個線程,線程獲取數據和分析用戶付款。然後使用AlarmManager設置通知和stopSelf。

public class NotificationService extends Service { 
... 
private AlarmManager am; 
private PendingIntent pi; 
private NotificationManager mNM; 
@Override 
public int onStartCommand(Intent intent, int flags, int startId) { 

Thread thread = new Thread(null, mTask, "AlarmService_Service"); 
    thr.start(); 

    return START_REDELIVER_INTENT; 
} 
Runnable mTask = new Runnable() { 
    public void run() { 

     List<Subscription> mDataList = getData; 

     if (mDataList.size() > 0) { 

      for (Subscription mSubscription : mDataList) { 


       if (mSubscription.isSub == true) { 

        Intent intent = new Intent(NotificationService.this, 
          AlamrReceiver.class); 
        intent.putExtra("data", (Serializable)mSubscription); 
        intent.setData(Uri.parse("custom://" + uniqueCode)); 
        intent.setAction(String.valueOf(uniqueCode)); 

        am = (AlarmManager) getSystemService(ALARM_SERVICE); 
        pi = PendingIntent.getBroadcast(
          NotificationService.this, uniqueCode, intent, 
          PendingIntent.FLAG_CANCEL_CURRENT); 
        am.set(AlarmManager.RTC_WAKEUP, reminderTime, pi); 
        uniqueCode = uniqueCode + 1; 

       } 
      } 

     } 

     NotificationService.this.stopSelf(); 
    } 
}; 
} 

2.接收廣播信息並顯示通知。

public class AlamrReceiver extends BroadcastReceiver { 
private NotificationManager mNM; 

@Override 
public void onReceive(Context context, Intent intent) { 
    // TODO Auto-generated method stub 
    mNM = (NotificationManager)  context.getSystemService(context.NOTIFICATION_SERVICE); 
    Subscription mSubscription = intent.getSerializableExtra("data"); 

    if (mSubscription != null) { 
    showNotification(context, mSubscription); 
    } 
} 
private void showNotification(Context context, Subscription mSubscription) { 
    ... 
    NotificationCompat.Builder builder = new NotificationCompat.Builder(
      context); 
    builder.setContentTitle(text); 
    builder.setContentText(subTitleString + currencyString); 
    builder.setSmallIcon(Common.CATEGORY_ICON[cIcon]); 
    builder.setDefaults(Notification.DEFAULT_VIBRATE); 
    builder.setAutoCancel(true); 

    Intent intent = new Intent(context, BillDetailsActivity.class); 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
    stackBuilder.addParentStack(BillDetailsActivity.class); 
    intent.putExtra("dataMap", (Serializable) tMap); 

    stackBuilder.addNextIntent(intent); 
    PendingIntent contentIntent = stackBuilder.getPendingIntent(0, 
      PendingIntent.FLAG_UPDATE_CURRENT); 

    builder.setContentIntent(contentIntent); 
    int uniqueRequestCode = (int) System.currentTimeMillis(); 
    mNM.notify(uniqueRequestCode, builder.build()); 

} 

}

  • 不要忘記BOOT_COMPLETED,手機重新啓動時,啓動服務和檢查數據庫
  • +0

    以及谷歌播放應用內結算是一個不錯的選擇,但由於我在我的地區在本地做我認爲我會嘗試數據庫的訂閱 – ElouCapitan

    1

    我不確定您的應用程序的概念,但如果您想檢查用戶是否訂閱了當前月份,則無需爲此運行Service

    你應該在你的應用程序的啓動畫面或主要活動中進行檢查。但無論如何, 如果你仍然需要這樣做,我建議去ServiceAlarmManagerBroadcastReceiver不會單獨工作,您需要根據您的需要在特定事件上觸發它們。

    另外,如果您使用的服務,請記住,Android的MIGHT殺死你的服務在低內存類的情況。

    所以我建議你應該去AlarmManager,這將在特定的時間或之後檢查訂閱狀態。

    PS:我知道這應該addded的評論,但我沒有足夠的信譽發表評論,以便張貼答案

    +0

    但AlarmManager願意執行AsyncTasks ? – ElouCapitan

    +0

    我想用AlarmManager的服務會很快爲你做這件事,在特定的時間設置一個警報,啓動訂閱檢查服務,一旦你得到響應,停止使用stopSelf()服務方法的服務。 –

    相關問題