2017-01-14 58 views
0

我正在構建一個應用程序,可以讀取傳入的短信。但是,我的BroadcastReceiver類無法檢測/讀取傳入的SMS。我認爲我的代碼是正確的,但仍然無法正常工作。BroadcastReceiver無法檢測/讀取短信

這裏是我的代碼:

[XML]

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.crashlocator.carlax.crashlocator"> 
<!-- 
    The ACCESS_COARSE/FINE_LOCATION permissions are not required to use 
    Google Maps Android API v2, but you must specify either coarse or fine 
    location permissions for the 'MyLocation' functionality. 
--> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 
<uses-permission android:name="android.permission.SEND_SMS" /> 
<uses-permission android:name="android.permission.RECEIVE_SMS" /> 
<uses-permission android:name="android.permission.READ_SMS" /> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /> 
<uses-permission android:name="android.permission.VIBRATE" /> 

<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" 
     android:theme="@android:style/Theme.NoTitleBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
    <!-- 
     The API key for Google Maps-based APIs is defined as a string resource. 
     (See the file "res/values/google_maps_api.xml"). 
     Note that the API key is linked to the encryption key used to sign the APK. 
     You need a different API key for each encryption key, including the release key that is used to 
     sign the APK for publishing. 
     You can define the keys for the debug and release targets in src/debug/ and src/release/. 
    --> 
    <meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="@string/google_maps_key" /> 

    <activity 
     android:name=".MapsActivity" 
     android:label="@string/title_activity_maps"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.default" /> 
     </intent-filter> 

    </activity> 
    <activity android:name=".MainActivity2" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.default" /> 
     </intent-filter> 
    </activity> 

    <receiver 
     android:name=".SmsReceiver" 
     android:exported="true" > 
     <intent-filter android:priority="2147483647" > 
      <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
     </intent-filter> 
    </receiver> 


</application> 

[JAVA CLASS]

public class SmsReceiver extends BroadcastReceiver { 

private static final String SMS_BUNDLE = "pdus"; 
private String smsBody, address; 

@Override 
public void onReceive(Context context, Intent intent) { 

    Toast.makeText(context,"Test",Toast.LENGTH_LONG); 
} 



public void showNotification(Context context) { 
    Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE); 

    PendingIntent contentIntent = PendingIntent.getActivity(context, 0, 
      new Intent(context, MapsActivity.class), 0); 

    v.vibrate(500); 
    Intent in = new Intent("SmsMessage.intent.MAIN"). 
      putExtra("get_msg", address + ":" + smsBody); 
    context.sendBroadcast(in); 

    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(context) 
        //.setSmallIcon(R.drawable.notif1) 
        .setContentTitle("Attention!") 
        .setContentText("Car crash occure."); 
    mBuilder.setContentIntent(contentIntent); 
    mBuilder.setDefaults(Notification.DEFAULT_SOUND); 
    mBuilder.setAutoCancel(true); 
    NotificationManager mNotificationManager = 
      (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    mNotificationManager.notify(1, mBuilder.build()); 

} 

} 
+0

正確Toast先用.show()然後驗證它。 –

回答

1

我想你忘了把.show()在吐司結束。

Toast.makeText(context, "Test" ,Toast.LENGTH_LONG).show(); 
+0

我明白了。感謝您的幫助。 :) –