安卓后台触发震动

huangapple go评论78阅读模式
英文:

Android make vibrate from background

问题

我使用以下代码使手机在接收特定文本的短信时振动。但只有当应用程序正在运行时,手机才会振动。如何让手机在应用程序关闭时也能振动?

清单权限:

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

SMSReceiver.java:

public class SMSReceiver extends BroadcastReceiver {

    private static final String TAG = "SmdReceiver";

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle myBundle = intent.getExtras();

        if (myBundle != null) {
            Object[] pdus = (Object[]) myBundle.get("pdus");

            for (int i = 0; i < pdus.length; i++) {
                SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
                String body = message.getMessageBody();
                if (body.equals("test")) {
                    Log.d(TAG, "vibrate"); // 在控制台打印
                    Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        v.vibrate(VibrationEffect.createOneShot(duration, VibrationEffect.DEFAULT_AMPLITUDE));
                    } else {
                        v.vibrate(duration); // 在API 26中已弃用
                    }
                }
            }
        }
    }
}

如果您有任何其他问题或需要进一步的帮助,请告诉我。

英文:

I use the code below to make the phone vibrate when I receive a SMS with a specific text. But the phone vibrate only if the app is running.
How can I make the phone vibrate even when the app is closed ?

Manifest Permission

&lt;uses-permission android:name=&quot;android.permission.READ_SMS&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.RECEIVE_SMS&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.SEND_SMS&quot; /&gt;
&lt;uses-permission android:name=&quot;android.permission.VIBRATE&quot; /&gt;

SMSReceiver.java

public class SMSReceiver extends BroadcastReceiver {

    private static final String TAG = &quot;SmdReceiver&quot;;

    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle myBundle = intent.getExtras();

        if (myBundle != null) {
            Object[] pdus = (Object[]) myBundle.get(&quot;pdus&quot;);

            for (int i = 0; i &lt; pdus.length; i++) {
                SmsMessage message = SmsMessage.createFromPdu((byte[]) pdus[i]);
                String body = message.getMessageBody();
                if (body == &quot;test&quot;) {
                    Log.d(TAG, &quot;vibrate&quot;); // is print in the console
                    Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
                    if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.O) {
                        v.vibrate(VibrationEffect.createOneShot(duration, VibrationEffect.DEFAULT_AMPLITUDE));
                    } else {
                        v.vibrate(duration); //deprecated in API 26
                    }
                }
            }
        }
    }
}

答案1

得分: 1

Android倾向于关闭后台应用,尤其是像MIUI这样的定制系统。为了避免这种情况,你需要将工作进程与永久通知关联,这样系统就不会关闭该应用。

还有其他方法可以保持应用程序的运行,比如服务(services)后台处理(background processing)

更多详细信息,请参阅后台执行限制(Background Execution Limits)

这是另一个关于类似问题的问题(question)

英文:

Android tend to kill background apps, especially customization like MIUI. To avoid this you need, for example, to attach the worker process to a permanent notification. So that the system won't kill the app.

There are other ways to keep the app running like services and background processing.

For more details follow Background Execution Limits.

And this is another question for something similar.

huangapple
  • 本文由 发表于 2020年10月16日 02:48:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64377949.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定