代码无效。通知应该每天一次

时间:2015-02-23 19:32:08

标签: java android xml

所以我不知道为什么这段代码不起作用。我想做一个"闹钟"通知每天会发生一次。只是想说我是新的Android。谢谢。

编辑:稍微更改了代码。警报方法执行,通知也执行,但我收到此错误消息:

  

-248 /? D / PowerManagerService:releaseWakeLock flags = 0x1 tag = AlarmManager W / ActivityManager:无法启动服务Intent {flg = 0x4 cmp = com.example.polakken.test / .lol(has extras)}:not found 06-13 00:00 :00.825 231-267 /? D / PowerManagerService:acquireWakeLock flags = 0x1 tag = AlarmManager 06-13 00:00:00.825 231-248 /? D / PowerManagerService:releaseWakeLock flags = 0x1 tag = AlarmManager -

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

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
        SharedPreferences.Editor editor = preferences.edit();
        int i = preferences.getInt("numberoflaunches", 1);

        if (i < 2) {
            alarmMethod();
            i++;
            editor.putInt("numberoflaunches", i);
            editor.commit();
        }

        if (savedInstanceState == null) {
            splashMethod();

        }




    }

//...

private void alarmMethod() {
        Intent intentbro = new Intent(this, lol.class);
        AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);

        PendingIntent pendingIntent = PendingIntent.getService(this, 0, intentbro, 0);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR, 0);
        calendar.set(Calendar.AM_PM, Calendar.AM);
        calendar.add(Calendar.DAY_OF_MONTH, 1);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 1000 * 60 * 60 * 24, pendingIntent);


        Toast.makeText(MainActivity.this, "start alarm", Toast.LENGTH_LONG).show();


    }

//notification class

public class lol extends Activity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        NotificationCompat.Builder b = new NotificationCompat.Builder(this);


        Intent intent1 = new Intent(this.getApplicationContext(), MainActivity.class);
        PendingIntent pIntent = PendingIntent.getActivity(this, 1, intent1, 0);


        b.setContentText("lol");
        b.setContentTitle("Default notification");
        b.setSmallIcon(R.drawable.iconography_small_size);
        b.setContentIntent(pIntent);







        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, b.build());




    }
    }

2 个答案:

答案 0 :(得分:0)

您要为警报添加额外的一天。它应该是这样的:

...
Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.HOUR_OF_DAY, 1);
        calendar.set(Calendar.AM_PM, Calendar.AM);

        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pendingIntent);
...

答案 1 :(得分:0)

您正在呼叫PendingIntent pendingIntent = PendingIntent.getService(this, 0, intentbro, 0);

但是,您需要在AndroidManifest中声明此服务,如下所示:

<service android:name=".Lol" />

此外,Java中的类应该大写,但您的Intent调用小写类:

Intent intentbro = new Intent(this, lol.class);

我建议您对代码运行静态分析,看看如何改进它!