我最近一直在做一个小项目,我遇到了一个小问题。
问题出现: 从其他设备发送消息时,设备状态不会更改为静默模式。
工作流:
短信包含" Msilent"从另一个设备发送> 受害者设备接收短信并读取字符串并识别关键字" Msilent" > 受害者电话响铃配置文件变为静音模式。设备Android版:Lollipop 5.0
以下是我的代码:
IncomingSms.java
package android.yu;
/**
* Created by Mahesh on 15-01-2017.
*/
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
import static android.content.Context.AUDIO_SERVICE;
/**
* Created by Mahesh on 09-01-2017.
*/
public class IncomingSms extends BroadcastReceiver {
private AudioManager maudio;
public void onReceive(Context context, Intent intent) {
maudio=(AudioManager) context.getSystemService(AUDIO_SERVICE);
Bundle myBundle = intent.getExtras();
SmsMessage[] messages = null;
String strMessage = "";
if (myBundle != null) {
Object[] pdus = (Object[]) myBundle.get("pdus");
messages = new SmsMessage[pdus.length];
for (int i = 0; i < messages.length; i++) {
String format = myBundle.getString("format");
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i], format);
/*strMessage += messages[i].getOriginatingAddress();
strMessage += " : ";*/
strMessage += messages[i].getMessageBody();
// strMessage += "\n";
}
Log.v("SMS", strMessage);
if (strMessage.contains("Msilent"))
maudio.setRingerMode(AudioManager.RINGER_MODE_SILENT);
Toast.makeText(context, strMessage, Toast.LENGTH_SHORT).show();
}
}
}
MainActivity.java
package android.yu;
import android.content.Context;
import android.media.AudioManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="android.yu">
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".IncomingSms">
<intent-filter android:priority="500">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
答案 0 :(得分:0)
您需要在主要活动中注册您的接收器。