2016-04-27 23 views
0
public class SchedulerEventService extends Service { 
    private static final String APP_TAG = "com.hascode.android.scheduler"; 
    Context contxt; 

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

    @Override 
    public int onStartCommand(final Intent intent, final int flags, 
      final int startId) { 
     Log.d(APP_TAG, "event received in service: " + new Date().toString()); 
     Toast.makeText(contxt, "Fire", 1000).show(); 

     return Service.START_NOT_STICKY; 
    } 

} 



public class SchedulerSetupReceiver extends BroadcastReceiver { 
    private static final String APP_TAG = "com.hascode.android"; 

    private static final int EXEC_INTERVAL = 20 * 1000; 

    @Override 
    public void onReceive(final Context ctx, final Intent intent) { 
     Log.d(APP_TAG, "SchedulerSetupReceiver.onReceive() called"); 
     AlarmManager alarmManager = (AlarmManager) ctx 
       .getSystemService(Context.ALARM_SERVICE); 
     Intent i = new Intent(ctx, SchedulerEventReceiver.class); // explicit 
                    // intent 
     PendingIntent intentExecuted = PendingIntent.getBroadcast(ctx, 0, i, 
       PendingIntent.FLAG_CANCEL_CURRENT); 
     Calendar now = Calendar.getInstance(); 
     now.add(Calendar.SECOND, 20); 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, 
       now.getTimeInMillis(), EXEC_INTERVAL, intentExecuted); 

     Toast.makeText(ctx, "Fire", 1000).show(); 
    } 

} 


public class SchedulerEventReceiver extends BroadcastReceiver { 
    private static final String APP_TAG = "com.hascode.android"; 

    @Override 
    public void onReceive(final Context ctx, final Intent intent) { 
     Log.d(APP_TAG, "SchedulerEventReceiver.onReceive() called"); 
     Intent eventService = new Intent(ctx, SchedulerEventService.class); 
     ctx.startService(eventService); 
     Toast.makeText(ctx, "Fire", 1000).show(); 
    } 

} 




<service 
      android:name="com.example.tataalarmmanger.SchedulerEventService" 
      android:process=":hascode_process" > 
     </service> 

     <receiver 
      android:name="com.example.tataalarmmanger.SchedulerEventReceiver" 
      android:process=":hascode_process" > 
     </receiver> 
     <receiver 
      android:name="com.example.tataalarmmanger.SchedulerSetupReceiver" 
      android:process=":hascode_process" > 
      <intent-filter> 
       <action android:name="android.intent.action.BOOT_COMPLETED" /> 
       <action android:name="android.intent.action.USER_PRESENT" /> 
      </intent-filter> 
     </receiver> 

これは私がこのコードを実行すると私のコードです。私はサービスを開始できません。私は、設定したサービスの動的時間そこから何度も火を鳴らす必要がある時間20秒ごとにアラームを鳴らします。時間を得るためにLOG.dをプッシュしましたが、日付が得られない場合は、どこに間違っているのかを教えてください。スケジュールアラームのためにバックグラウンドでサービスを開始できません。

+0

を拡張してみますが、マニフェストにこれらのすべてを定義していますか? –

+0

はい私のコードを見てください –

+0

@VivekMishraは私の質問のための解決策を持っています –

答えて

0

ではなく、単純なBroadcastReceiverのWakefulBroadcastReceiverを拡張

+0

私は時間を取る各20秒 –

+0

私は間違いをどこにしているか教えてもらえますか? –

0

のこのクラスはサービス

public class SchedulerEventService extends Service { 
    private static final String APP_TAG = "com.hascode.android.scheduler"; 
    Context contxt; 
private static final String APP_TAG = "com.hascode.android"; 

    private final long EXEC_INTERVAL = 20 * 1000; 

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

    @Override 
    public int onStartCommand(final Intent intent, final int flags, 
      final int startId) { 

PendingIntent intentExecuted = PendingIntent.getBroadcast(ctx, 0, i, 
       PendingIntent.FLAG_CANCEL_CURRENT); 
     Calendar now = Calendar.getInstance(); 
     now.add(Calendar.SECOND, 20); 
     alarmManager.setExactRepeating(AlarmManager.RTC_WAKEUP, 
       System.currentTimeMillis(), EXEC_INTERVAL, intentExecuted); 
     Log.d(APP_TAG, "event received in service: " + new Date().toString()); 
     Toast.makeText(contxt, "Fire", 1000).show(); 

     return Service.START_STICKY; 
    } 

} 
関連する問題