在BroadcastReceiver中使用sharedpreferences时无法编译数据

时间:2013-11-27 23:34:39

标签: android sms sharedpreferences

我有一个我想要无助的应用程序。我有一个设置布局/活动,将设置保存到共享pref(1个字符串和1个int)。我希望能够在用户收到短信时调用数据。以下是我的“接收”活动。

public class PassiveSms extends BroadcastReceiver {

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

    SharedPreferences sharedPref= getSharedPreferences("chaosdriver", Context.MODE_PRIVATE);
    int speedLimit = sharedPref.getInt("speedLimit", 1000);
    String message = sharedPref.getString("message", "I'm sorry, but I am driving. I will text you when I am able!");

    LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    Location lastKnownLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

        if (lastKnownLocation.getSpeed() > speedLimit)
        {
            Bundle extras = intent.getExtras();

            if (extras == null)
            {
                return;
            }

            abortBroadcast();

            Object[] pdus = (Object[]) extras.get("pdus");
            SmsMessage msg = SmsMessage.createFromPdu((byte[]) pdus[0]);

            String origNumber = msg.getOriginatingAddress();

            onSend(origNumber);

        }
    }

public void onSend(String px)
{
    String reply = "testest";
    SmsManager smsManager = SmsManager.getDefault();
    smsManager.sendTextMessage(px, null, reply, null, null);
}

}

这是我的清单:

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <receiver
        android:name="biz.midl.drivereply.PassiveLocationChangedReceiver"
        android:enabled="true" />

    <activity
        android:name="biz.midl.drivereply.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="biz.midl.drivereply.Settings"
        android:label="@string/title_activity_settings" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="biz.midl.drivereply.PassiveSms"
        android:label="@string/title_activity_passive_sms" >
    </activity>
    <receiver android:name="biz.midl.drivereply.SMSReceiver" >
        <intent-filter android:priority="999" >
            <action android:name="android.provider.Telephony.SMS_RECEIVED" />
        </intent-filter>
    </receiver>

    <service android:name=".MyService">
        <intent-filter>
            <action android:name="com.example.MyService"/>
        </intent-filter>
    </service>
</application>

“SharedPreferences sharedPref = getSharedPreferences(”chaosdriver“,Context.MODE_PRIVATE);”不允许我编译,因为“方法getSharedPreferences(String,int)未定义类型PassiveSms”

有什么建议吗?

1 个答案:

答案 0 :(得分:1)

你必须使用:

context.getSharedPreferences("chaosdriver", Context.MODE_PRIVATE);

由于SharedPreferences 仅可从Context 获得。 BroadcastReceiver具有适当的Context变量作为onReceive()方法的第一个参数,因此您可以使用“without questions”

现在它应该有效并解决你的问题。