Android 在收到短信时唤醒并通知

标签 android broadcastreceiver sleep

我正在尝试创建一个应用程序来接收带有特定内容的传入短信并显示通知(带声音),即使手机处于 sleep 模式也是如此。如果手机未处于 sleep 模式,该应用程序工作正常。但是一旦手机进入休眠状态,我会在手动唤醒手机后收到通知。我在 list 文件中有必要的权限,我也实现了 full_wake_lock 功能。

这是我的 list 文件:

    <?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>

主要 Activity :

    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);

        }

    }

}

唤醒锁:

    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 天时间才找到它... 我将不胜感激!

最佳答案

问题出在模拟器本身。甚至 rtc_wakeup 功能也不起作用。我在真实设备上试用了我的应用程序并且工作正常...

关于Android 在收到短信时唤醒并通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25245705/

相关文章:

android - EventSource 不会在 Android Firefox 上自动重新连接

java - 运行应用程序时出错 : No JDK specified

android - 仅向特定 Activity 发送广播

c++ - 如何将计时器添加到键盘记录器 C++

macos - 当我的 Mac 进入休眠状态时,我的应用程序会发生什么?

android - 如何在位图中绘制文本?

Android Studio 未检测到构建/运行时的代码更改(

android - BroadcastReceiver 生命周期——静态变量

android - 您是否错过了对 unregisterReceiver() 的调用?在安卓中

python - cronjobs 的性能比 sleep 函数更好吗?