1
我試圖檢測收到的SMS並通過texttospeech
讀取它。當在清單中聲明時,RECEIVE_SMS的廣播接收器不工作(靜態地)
當我宣佈廣播接收器在manifest
它不起作用。但是它在一個活動中動態完成時會起作用。
我知道某些廣播操作無法在清單中聲明並需要一個活動(如here所述)時被捕獲到接收方中,但在here中看到有人在清單中使用RECEIVE_SMS。
我不知道我在做什麼錯。任何幫助將不勝感激!
的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.bulsy.smstalk1">
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.READ_CONTACTS" />
<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="com.bulsy.smstalk1.SmsListener"
android:enabled="true"
android:permission="android.permission.BROADCAST_SMS"
android:exported="true">
<intent-filter android:priority="2147483647">//this doesnt work
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
SmsListener.java
public class SmsListener extends BroadcastReceiver{
private SharedPreferences preferences;
TextToSpeech textToSpeech;
String msg_from;
public SmsListener()
{
}
@Override
public void onReceive(final Context context, Intent intent) {
// TODO Auto-generated method stub
if(intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED")){
Bundle bundle = intent.getExtras(); //---get the SMS message passed in---
SmsMessage[] msgs = null;
if (bundle != null){
//---retrieve the SMS message received---
try{
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for(int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
msg_from = msgs[i].getOriginatingAddress();
final String msgBody = msgs[i].getMessageBody();
textToSpeech = new TextToSpeech(context, new TextToSpeech.OnInitListener() {
@Override
public void onInit(int status) {
if(status != TextToSpeech.ERROR) {
textToSpeech.setLanguage(Locale.UK);
String fromName = getContactName(context,msg_from);
fromName = fromName==null? msg_from:fromName;
textToSpeech.speak("You have a text message from " + fromName + ". Content: " + msgBody , TextToSpeech.QUEUE_FLUSH, null);
}
}
}
);
}
}catch(Exception e){
// Log.d("Exception caught",e.getMessage());
}
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SmsListener smsListener = new SmsListener();//Dynamically setting the receiver. this works.
IntentFilter filter = new IntentFilter();
filter.addAction("android.provider.Telephony.SMS_RECEIVED");
this.registerReceiver(smsListener,filter);
}
}
你確定''的'name'指向正確的地方嗎?當您測試清單Receiver時,是否在安裝後至少啓動一次「MainActivity」?順便說一句,''在''中是不必要的,''上不需要'enabled'和'exported'屬性。 –
接收器應該指向正確的位置。 com.bulsy.smstalk1.SmsListener。 'com.bulsy.smstalk1'是我的包名,'SmsListener'是我的類名。甚至用'.SmsListener'和'SmsListener'試過。沒有工作。我刪除了'啓用導出的類別'屬性。仍然沒有工作。 –
是的,'','exported'和'enabled'不會阻止它的工作。我只是說你不需要他們。如果Receiver和MainActivity位於同一個文件夾/包中,那麼這些'name'屬性都可以。另外,我的其他問題呢?當您測試清單Receiver時,您是否在安裝後運行一次'Activity'? –