英文:
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
<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 == "test") {
Log.d(TAG, "vibrate"); // is print in the console
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); //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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论