每天在Android上

时间:2015-08-18 13:29:56

标签: android notifications

我已经在网上找了几天这个答案。我对android java和stackoverflow都很陌生。

所以我想做的是每天12:30发送提示以提醒我的用户使用应用程序(如果可能的话,应该在没有运行应用程序的情况下发送通知) 。 我从这个网站上学到的是我必须创建一个扩展Service的notifyService类。

我已经尝试了我找到的所有解决方案,而且我从未收到过任何通知,所以我猜我的NotifyService类是错误的,或者我是以错误的方式调用它。

我已经在mainActivity上测试了我的通知创建(不使用notifyservice ),并在点击按钮时将其链接起来并且工作正常,所以我猜问题来自其他地方。

这是我的NotifiyService类:

public class NotifyService extends Service{
private int NOTIFICATION_ID = 2;

public IBinder onBind(Intent arg0) {
    // TODO Auto-generated method stub
    return null;
}

private Handler notificationTimer = new Handler() {
    @Override
    public void handleMessage(Message msg) {
        if(msg.what==0) {
            sendNotifications();
        }
        super.handleMessage(msg);
    }
};

private Runnable notificationCaller = new Runnable() {

    @Override
    public void run() {
        Message msg = notificationTimer.obtainMessage();
        msg.what = 0;
        notificationTimer.sendMessage(msg);
    }
};

public void sendNotifications(){

    Calendar calendar = Calendar.getInstance();
    Calendar timer = Calendar.getInstance();
    timer.set(Calendar.HOUR_OF_DAY, 12);
    timer.set(Calendar.MINUTE, 30);
    timer.set(Calendar.SECOND, 00);

    Long difference = calendar.getTimeInMillis() - timer.getTimeInMillis();

    if(difference<0){
        notificationTimer.postDelayed(notificationCaller, -(difference));
    }
    else{
        notificationTimer.postDelayed(notificationCaller, difference);
    }

    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://developer.android.com/reference/android/app/Notification.html"));
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);

    builder.setSmallIcon(R.drawable.ic_stat_notification);
    builder.setContentIntent(pendingIntent);
    builder.setAutoCancel(true);
    builder.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher));

    builder.setContentTitle("Pensez à vérifier votre glycémie !");
    builder.setContentText("Bonjour jeune Padawan, il est temps de penser à contrôler ta glycémie.");
    builder.setSubText("Click pour ouvrir Diab'App :)");

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(NOTIFICATION_ID, builder.build());
    notificationTimer.removeCallbacksAndMessages(null);
    notificationTimer.postDelayed(notificationCaller, 86400000);
}

}

这是我的mainActivity :(我不会把它全部链接起来,因为大部分都没有处理这个问题)

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    /* some code for my database init and layouts */

    Intent service = new Intent(this, NotifyService.class);
    this.startService(service);
}

我不确定这里的问题是什么,是因为我在onCreate函数上调用了startService,所以我必须启动我的应用程序来获取通知?

谢谢!

2 个答案:

答案 0 :(得分:0)

对于您要完成的任务,Android AlarmManager类,有一个更简单的解决方案......

答案 1 :(得分:0)

您需要在androidmanifest.xml中添加服务

<service android:name=".NotifyService"
                 android:enabled="true" />