广播接收器的Andr​​oid 4.0不影响共享preferences(可能3.1+) [英] Broadcast receiver not affecting shared preferences in Android 4.0 (probably 3.1+)

查看:117
本文介绍了广播接收器的Andr​​oid 4.0不影响共享preferences(可能3.1+)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我确实的答案,我的问题彻底搜查;通常我能找到的答案很容易pretty至pretty多的东西。

So I have indeed searched thoroughly for an answer to my question; normally I can find answers pretty easily to pretty much anything.

反正基本上我有一个警报管理设置最终设置的广播接收器。接收机内部,它决定意图已经被接收其中,删除一个共享preference,然后设置启动该活动的通知。问题是,我的手机用4.0的共享preference项目没有成功删除,但在任何previous手机我已经试过(2.2,2.3),它完美的作品。

Anyway, basically I have an alarm manager set up which eventually sets a broadcast receiver. Inside the receiver, it decides which intent has been received, removes a shared preference, and then sets a notification that starts the activity. The problem is that on my phones with 4.0 the shared preference item is not successfully deleted, but on any previous phones I've tried (2.2, 2.3) it works perfectly.

我做最终找到的Andr​​oid 3.1的文档和FLAG_INCLUDE_STOPPED_PACKAGES实施。我想扔了到意图,以防万一,但它仍然是行不通的。无论哪种方式,它不是,这是问题的活动的开展,但简单的删除的共享preference的。

I did end up finding the documentation of Android 3.1 and the FLAG_INCLUDE_STOPPED_PACKAGES implementation. I tried throwing that onto the intent, just in case, but it still wasn't working. Either way, it's not the launching of the activity that is the problem, but the simple deletion of a shared preference.

我希望这足够清楚了!我会在下面的一些code的。

I hope that's clear enough! I'll put in some of the code below.

这是哪里的意图已经启动:

This is where the intent is started:

Calendar cal = Calendar.getInstance();
int seconds = 5 * 60; // 1 * 24 * 60 * 60;
cal.add(Calendar.SECOND, seconds);

Intent intent = new Intent(SetAlertActivity.this, ReminderReceiver.class);
intent.putExtra("id", "FAlert");
//intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this.getApplicationContext(), FRAUD_ALERT_CODE, intent, 0);

AlarmManager alertManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alertManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent);

settingsEditor = alertSettings.edit();
settingsEditor.putLong("AlertTime1", cal.getTimeInMillis());
settingsEditor.commit();

和则广播接收器的onReceive():

And then the broadcast receiver onReceive():

    nContext = context;
    alertSettings = nContext.getSharedPreferences(MainActivity.PREFERENCE_FILENAME, 0);
    if (intent.getStringExtra("id").equals("FAlert"))
    {

        settingsEditor = alertSettings.edit();
        settingsEditor.remove("AlertTime1");
        settingsEditor.commit();

        String ns = Context.NOTIFICATION_SERVICE;
        int icon = R.drawable.ar_icon;
        CharSequence tickerText = nContext.getString(R.string.notification_ticker);
        CharSequence contentTitle = nContext.getString(R.string.notification_title);
        CharSequence contentText = nContext.getString(R.string.notification_text);
        long when = System.currentTimeMillis();

        NotificationManager mNotificationManager = (NotificationManager) nContext.getSystemService(ns);
        Notification notification = new Notification(icon, tickerText, when);

        Intent notificationIntent = new Intent(nContext, SetAlertActivity.class);
        PendingIntent contentIntent = PendingIntent.getActivity(nContext, 135, notificationIntent, 0);

        notification.defaults |= Notification.DEFAULT_SOUND;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.setLatestEventInfo(nContext, contentTitle, contentText, contentIntent);
        mNotificationManager.notify(NOTIFICATION_ID, notification);
    }

所以,我在4.0之前提到的,在我的设备(我没有任何3.x设备)的

So, as I mentioned before, on my devices on 4.0 (I don't have any 3.X devices) the

settingsEditor = alertSettings.edit();
settingsEditor.remove("AlertTime1");
settingsEditor.commit();

部分不工作。该活动将正常打开,但AlertTime1依然存在。在2.2和2.3的设备的AlertTime1被成功删除。

part isn't working. The activity will open correctly, but the "AlertTime1" is still there. On the 2.2 and 2.3 devices, the "AlertTime1" is successfully deleted.

叹息的:D

感谢您的帮助!

哦,万一它需要的,这里是我的清单用于接收器:

Oh, and in case it's needed, here is my manifest for the receiver:

<receiver
    android:name="ReminderReceiver"
    android:process=":remote" >
</receiver>

这是区别在哪里显示:

    alertSettings = getSharedPreferences(AlertRenewActivity.PREFERENCE_FILENAME, 0);
    settingsEditor = alertSettings.edit();
    if (alertSettings.contains("AlertTime1"))
    {
        alertTime = alertSettings.getLong("AlertTime1", 0);
        timeLeft = (int) ((alertTime - System.currentTimeMillis()) / (1000L));
        daysLeft = timeLeft / (60 * 60 * 24);
        daysLeftView.setText(Integer.toString(daysLeft));
        setAlert.setEnabled(false);
        setAlert.setTextColor(R.color.dark_text);
    }
    else
    {
        daysLeftView.setText(R.string.no_alert_set);
    }

在我的旧手机,它正确地重置为说不报警集,但是,从4.0的手机仍然显示左0的日子(这是它说什么,因为我是唯一的警报设置为5分钟左右用于测试)。基本上,用户不能设置一个新的警报,因为它没有正确重置,并再次,仅在4.0的手机我想:P

On my older phones, it correctly resets to saying "No Alert Set" but on the 4.0 phones it still shows "0" days left (which is what it says since I'm only setting the alert to 5 minutes or so for testing). Basically, the user can't set a new alert because it hasn't reset correctly, and again, only on the 4.0 phones I'm trying :P

推荐答案

我最终什么事做的只是有在说活动本身的支票如果报警时间小于0,删除闹钟时间。这是围绕一个工作;我没有收到,不幸的是工作的其他答案。

What I ended up doing was just having a check in the activity itself that says "if the alarm time is less than 0, delete the alarm time." It was a work around; I didn't receive any other answer that worked unfortunately.

这篇关于广播接收器的Andr​​oid 4.0不影响共享preferences(可能3.1+)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆