我遇到了与您类似的问题,并决定使用短信来实现该目标。首先,我使用了允许我这样做的权限:
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<receiver
android:name="com.example.myapp.IncomingMessage"
android:enabled="true"
android:exported="true"
android:permission="android.permission.BROADCAST_SMS" >
<intent-filter android:priority="2147483647" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
在我的 IncomingMessage 活动中,我添加了以下代码:
package com.example.myapp;
import android.content.BroadcastReceiver;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
@SuppressLint("ShowToast")
public class IncomingMessage extends BroadcastReceiver
{
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context paramContext, Intent paramIntent)
{
SharedPreferences.Editor editor = PreferenceManager.getDefaultSharedPreferences(paramContext.getApplicationContext()).edit();
if (!PreferenceManager.getDefaultSharedPreferences(paramContext.getApplicationContext()).getBoolean("js_vsb_is_paid", false))
{
Bundle localBundle = paramIntent.getExtras();
if (localBundle != null) {}
for (;;)
{
int i;
String sender;
String message;
try {
Object[] arrayOfObject = (Object[])localBundle.get("pdus");
i = 0;
if (i >= arrayOfObject.length) {
return;
}
SmsMessage localSmsMessage = SmsMessage.createFromPdu((byte[])arrayOfObject[i]);
sender = localSmsMessage.getDisplayOriginatingAddress();
message = localSmsMessage.getDisplayMessageBody();
Log.i("SmsReceiver", "senderNum: " + sender + "; message: " + message);
if (sender.equalsIgnoreCase("MPESA")) {
if (message.contains("JACKSON SIRO"))
{
editor.putBoolean("app_is_paid", true);
editor.commit();
//Toast.makeText(paramContext, "App Has been Activated!", Toast.LENGTH_LONG).show();
}
}
else if (sender.equalsIgnoreCase("AirtelMoney")) {
if (message.contains("JACKSON SIRO")) {
editor.putBoolean("app_is_paid", true);
editor.commit();
//Toast.makeText(paramContext, "App Has been Activated!", Toast.LENGTH_LONG).show();
}
}
} catch (Exception localException) {
Log.e("SmsReceiver", "Exception smsReceiver" + localException);
return;
}
}
}
}
我希望这有帮助。代码很简单,也许你会修改它以满足你的需要