警报唤醒锁定在服务中不起作用

时间:2015-07-15 23:40:00

标签: android service alarmmanager alarm android-wake-lock

我正在尝试创建一个警报,其中包含我已经存储在Parse中的时间列表。到目前为止,我能够让闹钟响起。但现在我希望它即使在设备处于睡眠状态时也会关闭。我知道我需要将wakelock实现到我已经做过的服务中。我也明白我需要包含“FLAG_SHOW_WHEN_LOCKED | FLAG_TURN_SCREEN_ON,FLAG_FULLSCREEN”。我只知道使用get Window的唯一方法。但我知道getWindow不是为了服务。所以,我需要一些帮助来解决这个问题。

我有一个带有此代码的AlarmService:

public View getView(int position, View convertView, ViewGroup parent) {

        if(convertView == null){
            final ImageItem imageItem = (ImageItem) getItem(position);
            LayoutInflater li = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            LinearLayout itemLayout = (LinearLayout) li.inflate(R.layout.image_item, parent, false);

            final TextView nameView = (TextView) itemLayout.findViewById(R.id.nameView);
            nameView.setText(imageItem.getName());

            final ImageView imageView = (ImageView) itemLayout.findViewById(R.id.imageView);
            String url = imageItem.getUrl();
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;
            //Bitmap bitmap = BitmapFactory.decodeFile(url);
            Drawable d = Drawable.createFromPath(url);
            imageView.setImageDrawable(d);
            imageView.setLayoutParams(new LinearLayout.LayoutParams(WIDTH, HEIGHT));
            imageView.setPadding(PADDING, PADDING, PADDING, PADDING);
            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);

            return itemLayout;
        } else {
            return convertView;
        }

    }

按下保存按钮后,服务将关闭,按下按钮添加警报活动:

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    super.onStartCommand(intent, flags, startId);
    Toast.makeText(this,"onStartCommand()",Toast.LENGTH_SHORT).show();

    //wake lock
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    mWakeLock = pm.newWakeLock(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, "My Wake Log");
    mWakeLock.acquire();

    //This code below doesn't work because getWindow is not for Service. what is my other option?
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON,
                    WindowManager.LayoutParams.FLAG_FULLSCREEN |
                    WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
                    WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);
    setContentView(R.layout.activity_reminder_screen);

    //start alarm screen
    Intent intent = new Intent(this, AlarmScreenActivity.class);
    ReminderIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);

    return flags;
}

提前谢谢

更新 我尝试将我的服务转换为一个Activity并将此代码用于intsead:

//set time into calendar instance
Calendar calendar= Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,mAlarmDetails.timeHour);
calendar.set(Calendar.MINUTE,mAlarmDetails.timeMinute);

AlarmManager reminder = (AlarmManager)getSystemService(Context.ALARM_SERVICE);

Intent intent = new Intent(this, AlarmService.class);
PendingIntent pendingIntent = PendingIntent.getService(this,0,intent,PendingIntent.FLAG_UPDATE_CURRENT);

reminder.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),pendingIntent);

我还将挂起意图中的getService从按钮更改为getActivity。但在我改变这些后,没有任何东西出现。警报没有出现。

2 个答案:

答案 0 :(得分:0)

您无需保持唤醒锁定。只有在您想要阻止设备进入睡眠状态时才这样做。在您的情况下,您只需要设备唤醒您。使用RTC_WAKEUP或ELAPSED_REALTIME_WAKEUP将实现此目标。您可以在唤醒时获取唤醒锁定,以防止设备在异步处理事件时立即重新进入休眠状态。

RTC_WAKEUP is not working

  

RTC_WAKEUP不会打开屏幕,它只会唤醒你   cpu让你的工作完成。要打开屏幕,您需要   一个完整的唤醒锁被收购。

答案 1 :(得分:0)

您需要wakeful intent service或WakefulBroadcastReceiver - 将待定意图更改为广播(PendingIntent pendingIntent = PendingIntent.getBroadcast...)设置接收器并在接收器中启动唤醒意向服务 - 或设置WakefulBroadcastReceiver。警报管理器在onReceive运行时保留唤醒锁 - 通过使用WIS(或WBR,没有使用它们),你基本上获得了一个在onReceive端和你的服务启动之间活动的唤醒锁。请参阅:https://stackoverflow.com/a/20332558/281545