为什么警报管理器不起作用?

时间:2017-02-02 08:15:27

标签: android

我有一项改变壁纸的服务。我加了警报 经理每天重复一遍但不起作用。

按钮点击:

ON1 ="ON";
Customthemes.AppPrefrances.getInstance(getActivity()).setON1(ON1);
Intent start = new Intent (getActivity(),Changeimage.class);
getActivity().startService(start);

在服务中的startcommand:

 Calendar calendar = Calendar.getInstance();
        int day = calendar.get(Calendar.DAY_OF_WEEK);

        switch (day) {


            case Calendar.FRIDAY:
                Bitmap bitmap5 = BitmapFactory.decodeFile(check5);
                WallpaperManager myWallpaperManager1
                        = WallpaperManager.getInstance(getApplicationContext());
                try {
                    myWallpaperManager1.setBitmap(bitmap5);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            case Calendar.MONDAY:
                Bitmap bitmap1 = BitmapFactory.decodeFile(check);
                WallpaperManager myWallpaperManager2
                        = WallpaperManager.getInstance(getApplicationContext());
                try {
                    myWallpaperManager2.setBitmap(bitmap1);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            case Calendar.SATURDAY:
                Bitmap bitmap6 = BitmapFactory.decodeFile(check6);
                WallpaperManager myWallpaperManager3
                        = WallpaperManager.getInstance(getApplicationContext());
                try {
                    myWallpaperManager3.setBitmap(bitmap6);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            case Calendar.SUNDAY:
                Bitmap bitmap7 = BitmapFactory.decodeFile(check7);
                WallpaperManager myWallpaperManager4
                        = WallpaperManager.getInstance(getApplicationContext());
                try {
                    myWallpaperManager4.setBitmap(bitmap7);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            case Calendar.THURSDAY:
                Bitmap bitmap4 = BitmapFactory.decodeFile(check4);
                WallpaperManager myWallpaperManager5
                        = WallpaperManager.getInstance(getApplicationContext());
                try {
                    myWallpaperManager5.setBitmap(bitmap4);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            case Calendar.TUESDAY:
                Bitmap bitmap2 = BitmapFactory.decodeFile(check2);
                WallpaperManager myWallpaperManager6
                        = WallpaperManager.getInstance(getApplicationContext());
                try {
                    myWallpaperManager6.setBitmap(bitmap2);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
            case Calendar.WEDNESDAY:
                Bitmap bitmap3 = BitmapFactory.decodeFile(check3);
                WallpaperManager myWallpaperManager7
                        = WallpaperManager.getInstance(getApplicationContext());
                try {
                    myWallpaperManager7.setBitmap(bitmap3);
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                break;
        }
        Calendar calendar2 = Calendar.getInstance();
        calendar2.setTimeInMillis(System.currentTimeMillis());
        calendar2.set(Calendar.SECOND, 0);
        calendar2.set(Calendar.MINUTE, 0);
        calendar2.set(Calendar.HOUR, 0);
        calendar2.set(Calendar.AM_PM, Calendar.AM);
        calendar2.add(Calendar.DAY_OF_MONTH, 1);

        Intent intentalarm = new Intent(getApplicationContext(), AlarmReceiver.class);
        intentalarm.setAction("packagename.ACTION");
        PendingIntent pendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
                0, intentalarm, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarm = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
        alarm.cancel(pendingIntent);
        alarm.setRepeating(AlarmManager.RTC_WAKEUP, calendar2.getTimeInMillis(), 24 * 60 * 60 * 1000, pendingIntent);
    }
    return START_STICKY;

AlarmReceiver:

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


    String action = intent.getAction();
    if (SOMEACTION.equals(action)) {
        Intent start = new Intent (context,Changeimage.class);
        context.startService(start);
    }
}

可能是什么问题?为什么不起作用?

1 个答案:

答案 0 :(得分:0)

在此清单中注册您的接收器

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

像这样发送

    Intent intentalarm = new Intent(getApplicationContext(), AlarmReceiver.class);
            intentalarm.putExtra("packagename"

);

得到这样的

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


    String action = intent.getStringExtra("packagename");
    if (SOMEACTION.equals(action)) {
        Intent start = new Intent (context,Changeimage.class);
        context.startService(start);
    }
}