BroadcastReceiver无法检测/读取短信

时间:2017-01-14 14:00:32

标签: java android

我正在构建一个可以读取传入短信的应用。但是,我的BroadcastReceiver类无法检测/读取传入的SMS。我认为我的代码是正确的,但它仍然无法正常工作。

这是我的代码:

[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());

}

}

1 个答案:

答案 0 :(得分:1)

我想你忘了把.show()放在Toast的末尾。

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