2017-05-03 11 views
-1

デバイス起動時またはアプリ起動時に、notifiy()という機能をNotificationServiceに起動するこのアプリがあります。 どうか私はこのnotifiy()関数を毎日特定の時間に行うことができます(午前12時、午前3:00)で、私はしばらくの間、検索し、私が見たすべてはAlarmManagerで作業しているが、私は私のコードでそれを使用する方法を理解していないAndroid - 毎日の通知

MainActivity

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     startService(new Intent(this,NotificationService.class)); 

BootReceiver

public class BootReceiver extends BroadcastReceiver{ 
    @Override 
    public void onReceive(Context context, Intent intent) { 
     context.startService(new Intent(context,NotificationService.class)); 
    } 
} 

NotificationService

public class NotificationService extends Service { 
    private MediaPlayer mediaPlayer; 
    @Nullable 
     @Override 
     public IBinder onBind(Intent intent) { 
      return null; 
     } 

     @Override 
     public void onCreate() { 
      super.onCreate(); 
      notifiy(); 
     } 
     @Override 
    public int onStartCommand(Intent intent, int flags, int startId) { 
     try { 

     }catch (Exception e){ 
      e.printStackTrace(); 
     } 
     return super.onStartCommand(intent, flags, startId); 
    } 
    @Override 
    public void onDestroy() { 
     try { 

     }catch (Exception e){ 
      e.printStackTrace(); 
     } 
     Intent intent=new Intent("com.company.app"); 
     intent.putExtra("yourvalue","torestore"); 
     sendBroadcast(intent); 
    } 
    public void notifiy(){ 
     IntentFilter intentFilter=new IntentFilter(); 
     intentFilter.addAction("RSSPullService"); 
     Intent mIntent=new Intent(Intent.ACTION_VIEW, Uri.parse("")); 
     PendingIntent pendingIntent=PendingIntent.getActivity(getBaseContext(),0,mIntent,Intent.FLAG_ACTIVITY_NEW_TASK); 
     Context context=getApplicationContext(); 
     Notification.Builder builder; 
     builder=new Notification.Builder(context) 
       .setContentTitle(title) 
       .setContentText("") 
       .setContentIntent(pendingIntent) 
       .setDefaults(Notification.DEFAULT_SOUND) 
       .setAutoCancel(true) 
       .setSmallIcon(R.drawable.images); 
     Notification notification=builder.build(); 
     NotificationManager notificationManager=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); 
     notificationManager.notify(1,notification); 
     mediaPlayer = MediaPlayer.create(this, R.raw.msound); 
     mediaPlayer.setOnPreparedListener(new MediaPlayer.OnPreparedListener() { 
      @Override 
      public void onPrepared(MediaPlayer mp) { 
       mediaPlayer.start(); 
      } 
     }); 
    } 
} 

答えて

0

1.あなたはlaunchingアプリ後alarmを設定したいなら、あなたはあなたのMainActivity'sonCreate()方法でコードの下に追加することができます。

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    // Schedule Date & time 
    Calendar target = Calendar.getInstance(); 
    target.set(2017, 5, 3, 12, 0, 0); 

    // Intent 
    Intent mIntent = new Intent(getApplicationContext(), AlarmReceiver.class); 
    mIntent.putExtra("MSG_ID", "SOME MESSAGE"); 

    // Pending broadcast intent 
    PendingIntent mPI = PendingIntent.getBroadcast(getApplicationContext(), 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

    // Alarm manager 
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

    // Set alarm 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, target.getTimeInMillis(), AlarmManager.INTERVAL_DAY, mPI);  

} 

2.あなたはboot完了後alarmを設定したい場合は、あなたがBootReceiver'sonReceive()方法でそのコードを追加することができます。

import android.content.BroadcastReceiver; 
import android.content.Context; 
import android.content.Intent; 
import android.util.Log; 


public class AlarmReceiver extends BroadcastReceiver { 

    @Override 
    public void onReceive(Context context, Intent intent) 
    { 

     context.startService(new Intent(context, NotificationService.class)); 
     Log.d("AlarmReceiver", "Called "); 
    } 
} 

宣言AlarmReceiverクラス:ここ

public class BootReceiver extends BroadcastReceiver{ 

    @Override 
    public void onReceive(Context context, Intent intent) { 

     // Schedule Date & time 
     Calendar target = Calendar.getInstance(); 
     target.set(2017, 5, 3, 12, 0, 0); 

     // Intent 
     Intent mIntent = new Intent(getApplicationContext(), AlarmReceiver.class); 
     mIntent.putExtra("MSG_ID", "SOME MESSAGE"); 

     // Pending broadcast intent 
     PendingIntent mPI = PendingIntent.getBroadcast(getApplicationContext(), 0, mIntent, PendingIntent.FLAG_UPDATE_CURRENT); 

     // Alarm manager 
     AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

     // Set alarm 
     alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, target.getTimeInMillis(), AlarmManager.INTERVAL_DAY, mPI); 

    } 

} 

AlarmReceiverクラスですin AndroidManifest.xml

<receiver 
    android:name="YOUR_PACKAGE.AlarmReceiver"> 

</receiver> 
+0

その放送受信機。私の更新された答えを確認してください – FAT

+0

アラームトリガーAlarmReceiverのonReceive()メソッドが呼び出されるとき – FAT

+0

AlarmReveiverはsmy BootReceiverクラスと同じですか? –

関連する問題