这是MainActivity.java
:如何在收到短信时使用广播接收器吐司?
package tagit.aj.com.broadcastreceiverforsms;
import android.content.Intent;
import android.content.IntentFilter;
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);
}
}
此所述MyReceiver
类,其包括广播接收器的方法onReceive
。这里使用的一种方法是“不推荐”,但我认为它不会在测试中产生任何问题。
package tagit.aj.com.broadcastreceiverforsms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class MyReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context,"IncomingSms",Toast.LENGTH_SHORT).show();
// Retrieves a map of extended data from the intent.
final Bundle bundle = intent.getExtras();
try {
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdus.length; i++) {
SmsMessage messages = SmsMessage.createFromPdu((byte[]) pdus[i]);
String phoneNumber = messages.getDisplayOriginatingAddress();
String phone = phoneNumber;
String stringMessage = messages.getDisplayMessageBody();
int duration = Toast.LENGTH_LONG;
Log.i("Broadcasting", "Number" + phone + "Message" + stringMessage);
Toast toast = Toast.makeText(context, "senderNum: " + phone + ", message: " + stringMessage, duration);
toast.show();
} // end for loop
} // bundle is null
} catch (Exception e) {
Log.e("SmsReceiver", "Exception smsReceiver" + e);
}
}
}
的XML布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="tagit.aj.com.broadcastreceiverforsms.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
而且AndroidManifest文件是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="tagit.aj.com.broadcastreceiverforsms">
<uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
<uses-permission android:name="android.permission.READ_SMS"></uses-permission>
<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=".MyReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED"></action>
</intent-filter>
</receiver>
</application>
</manifest>
请为我提供为什么发生此问题,并且解决方案(如果有的话)。我在Android KitKat上运行这个应用程序。
你从来没有真正解释你的问题是什么所做的更改。 – Bryan
@Bryan ...我的问题是代码不起作用 –
*什么*不适用于您的代码?你期待代码做什么,它做的是什么?你很幸运@Lev似乎和你自己有类似的问题,并且能够提供解决方案。如果你不提供[mcve],情况并非总是如此。如果它解决了您的问题,您应该接受该答案。 – Bryan