如何每分钟向android发送一个通知

时间:2015-07-22 15:23:09

标签: java android time notifications period

我尝试制作一个向android发送自定义通知的应用程序,该部分有效,但后来我想实现一项服务,以便应用程序每分钟发送一次通知。但是我错过了什么,你们可以帮助我吗?非常感谢!错误指向"这个",我是Android编程的新手,我真的不知道如何解决这个问题。

public class MainActivity extends Activity {

public class notifService extends Service {
    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();

    public IBinder onBind(Intent arg0) {
        return null;
    }


    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        final Intent intent1 = new Intent(this, notifService.class);

        scheduler.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                setContentView(R.layout.activity_main);
                //Create PendingIntent
                PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent1, PendingIntent.FLAG_UPDATE_CURRENT);
                //Create Notification Action
                NotificationCompat.Action action = new NotificationCompat.Action.Builder(
                        R.mipmap.app_icon, getString(R.string.wearTitle), pendingIntent).build();
                //Create Notification
                Notification notification = new NotificationCompat.Builder(this)
                        .setContentText(getString(R.string.content))
                        .setContentTitle(getString(R.string.title))
                        .setSmallIcon((R.mipmap.app_icon))
                        .extend(new NotificationCompat.WearableExtender().addAction(action))
                        .build();
                //Create Notification Manager
                NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(this);
                notificationManagerCompat.notify(001, notification);
            }
        }, 60, 60, TimeUnit.SECONDS);

0 个答案:

没有答案
相关问题