Android - 每日通知

时间:2017-05-03 13:53:49

标签: java android push-notification alarmmanager

我有这个应用程序,当设备启动或应用程序打开时,它会在NotificationService中启动函数notifiy() 我的问题是我怎么能在每天的特定时间制作这个notifiy()函数,例如在(上午12点,凌晨3点)我搜索了一段时间,我看到的只是使用AlarmManager,但我没有&#39 ;了解如何在我的代码中使用它

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();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

1。如果您想在alarm应用后设置launching,则可以在MainActivity's onCreate()方法中添加以下代码:< / p>

@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。如果您想在alarm完成后设置boot,那么您可以使用BootReceiver's onReceive()方法添加该代码:< / p>

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类:

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中声明AndroidManifest.xml课程:

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

</receiver>