我正在尝试创建一个应用程序来接收带有特定内容的传入短信,并显示通知(带声音),即使手机处于睡眠模式。如果手机未处于睡眠模式,该应用程序正常工作。但是一旦手机处于睡眠状态,我会在手动唤醒手机后收到通知。我在清单文件中拥有必要的权限,并且我还实现了full_wake_lock函数。
这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidexample.broadcastreceiver"
android:versionCode="1"
android:versionName="1.0" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:debuggable="true"
android:theme="@style/AppTheme" >
<activity
android:name=".BroadcastNewSms"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".IncomingSms"
android:permission="android.permission.BROADCAST_SMS">
<intent-filter android:priority="2147483647" >
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.DISABLE_KEYGUARD" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.VIBRATE" />
</manifest>
MainActivity:
public class BroadcastNewSms extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("LOG'", " - broadcast created");
setContentView(R.layout.androidexample_broadcast_newsms);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
}
广播接收器:
public class IncomingSms extends BroadcastReceiver {
final SmsManager sms = SmsManager.getDefault();
public void onReceive(Context context, Intent intent) {
Log.i("LOG'"," - onreceive called");
WakeLocker.acquire(context);
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
final Object[] pdusObj = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdusObj.length; i++) {
SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
String phoneNumber = currentMessage.getDisplayOriginatingAddress();
String senderNum = phoneNumber;
String message = currentMessage.getDisplayMessageBody();
if (message.equals("test")) {
Log.i("SmsReceiver", "senderNum: " + senderNum + "; message: " + message);
AudioManager audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_RING);
audioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audioManager.setStreamVolume(AudioManager.STREAM_RING, maxVolume, AudioManager.FLAG_SHOW_UI + AudioManager.FLAG_PLAY_SOUND);
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, "Sender: "+ senderNum + ", Message: " + message, duration);
toast.show();
final int NOTIF_ID = 1234;
NotificationManager notifManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification note = new Notification(R.drawable.ic_launcher, "New sms", System.currentTimeMillis());
PendingIntent intent2 = PendingIntent.getActivity(context, 0, new Intent(context, IncomingSms.class), 0);
note.setLatestEventInfo(context, "New sms", "You have one unread message.", intent2);
notifManager.notify(NOTIF_ID, note);
Log.i("LOG'"," - notified");
// notifManager.cancel(NOTIF_ID);
WakeLocker.release();
//abortBroadcast();
}
}
}
} catch (Exception e) {
Log.e("SmsReceive", "Exception" +e);
}
}
}
WakeLocker:
public abstract class WakeLocker {
private static PowerManager.WakeLock wakeLock;
public static void acquire(Context ctx) {
if (wakeLock != null) wakeLock.release();
PowerManager pm = (PowerManager) ctx.getSystemService(Context.POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK |
PowerManager.ACQUIRE_CAUSES_WAKEUP |
PowerManager.ON_AFTER_RELEASE, "mysms1");
wakeLock.acquire();
}
public static void release() {
if (wakeLock != null) wakeLock.release(); wakeLock = null;
}
}
我真的不知道会出现什么问题,但我花了3天才找到它...... 我会感激任何帮助!
答案 0 :(得分:1)
问题在于模拟器本身。即使rtc_wakeup功能也无效。我在真实的设备上试过我的应用程序并且工作正常......