Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 收到短信时,在toast中读取收到的短信_Java_Android - Fatal编程技术网

Java 收到短信时,在toast中读取收到的短信

Java 收到短信时,在toast中读取收到的短信,java,android,Java,Android,我想设计一个android应用程序,在android toast中显示收到的短信。 我正在使用android studio和android 5.0 lolipop进行编译。 以下是我创建应用程序的步骤。(所有步骤均来自此站点) 创建一个新的android项目 输入应用程序名称和位置 选择最小sdk为“android 2.0” 单击添加“无活动” 在下创建一个SMSReceiver.java文件 android->app->java->com.example……->SMSReceiver.jav

我想设计一个android应用程序,在android toast中显示收到的短信。 我正在使用android studio和android 5.0 lolipop进行编译。 以下是我创建应用程序的步骤。(所有步骤均来自此站点)

  • 创建一个新的android项目
  • 输入应用程序名称和位置
  • 选择最小sdk为“android 2.0”
  • 单击添加“无活动”
  • 在下创建一个SMSReceiver.java文件 android->app->java->com.example……->SMSReceiver.java
使用以下代码

package com.example.vaibhav.savemev1;
import java.lang.*;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;
public class SMSReceiver extends BroadcastReceiver {

// Get the object of SmsManager

final SmsManager sms = SmsManager.getDefault();

public void onReceive(Context context, Intent intent) {

    // Retrieves a map of extended data from the intent.
    final Bundle bundle = intent.getExtras();

    try {

        if (bundle != null) {

            final Object[] pdusObj = (Object[]) bundle.get("pdus");

            for (int i = 0; i < pdusObj.length; i++) {

                SmsMessage currentMessage = SmsMessage.createFromPdu((byte[]) pdusObj[i]);
                String phoneNumber = currentMessage.getDisplayOriginatingAddress();

                String senderNum = phoneNumber;
                String message = currentMessage.getDisplayMessageBody();

                // Show Alert
                int duration = Toast.LENGTH_LONG;
                Toast toast = Toast.makeText(context,
                        "senderNum: " + senderNum + ", message: " + message, duration);
                toast.show();

            } // end for loop
        } // bundle is null

    } catch (Exception e) {
        Log.e("SmsReceiver", "Exception smsReceiver" + e);
    }
}
}
package com.example.vaibhav.savemev1;
导入java.lang.*;
导入android.content.BroadcastReceiver;
导入android.content.Context;
导入android.content.Intent;
导入android.os.Bundle;
导入android.telephony.smsmsmanager;
导入android.telephony.sms消息;
导入android.util.Log;
导入android.widget.Toast;
公共类SMSReceiver扩展了BroadcastReceiver{
//获取SmsManager的对象
final smsmsmanager sms=smsmsmanager.getDefault();
公共void onReceive(上下文、意图){
//从意图检索扩展数据的映射。
final Bundle=intent.getExtras();
试一试{
if(bundle!=null){
最终对象[]pdusObj=(对象[])bundle.get(“pdus”);
对于(int i=0;i
AndroidManifest.xml代码是

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vaibhav.savemev1">
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<uses-permission android:name="android.permission.READ_SMS"/>
<application android:allowBackup="true" android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher" android:theme="@style/AppTheme">
    <receiver android:name=".SMSReceiver">
        <intent-filter>
            <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
        </intent-filter>
    </receiver>
</application>

  • 点击“安卓AVD”并“擦除”安卓虚拟设备中的所有数据
  • 单击播放按钮并在编辑配置窗口中 ->活动->“不启动活动”单选按钮。点击它
  • 好的
  • 选择设备->启动先前已擦除数据的设备

仍然没有成功。我做错什么了吗?该应用程序正在成功编译,但在收到短信时不会显示任何土司。

一个好的答案是:你能说一下你是如何测试的吗?你使用模拟器和模拟短信吗?如果是这样,您将需要设置AndroidManifest中接收到的数据

<receiver android:name=".SMSReceiver">
    <intent-filter>
        <action android:name="android.provider.Telephony.SMS_RECEIVED"/>
    </intent-filter>
    <!-- extent another intent-filter -->
    <intent-filter>
            <action android:name="android.intent.action.DATA_SMS_RECEIVED" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:scheme="sms" />
            <data android:host="localhost" />
            <data android:host="16984" />
        </intent-filter>
</receiver>

使用此功能,您可以使用ddms或简单的telnet向虚拟设备发送SMS。像这样:

 telnet localhost 16984

 sms send <Hello World>
telnet本地主机16984
短信发送
应显示带括号的文本。这只适用于虚拟设备。在设备上,接收到的电话短信应该可以做到这一点


如果它不起作用,您应该使用logcat并在那里发布信息表单

您是否集成了日志以帮助调试此问题?如果
Toast
消息依赖于
pdusObj
,那么这似乎是有问题的。我用一个Toast代码替换了完整的“public void onReceive(Context-Context,Intent-Intent)”代码。这就是“Toast.makeText(上下文,“hi!sms已收到”,Toast.LENGTH_LONG).show();”,但它仍然没有在收到sms时向我显示说“hi!sms已收到”的Toast。我将我的替换为您在评论中所说的内容。我刚把端口号从16984改为5554。但仍然没有成功。logcat是空的。我使用telnet向我的虚拟设备发送短信,这是android studio提供给我的。我发现了一个问题:你只是在实现一个广播接收器,对吗?是的。所以没有办法在启动状态下安装BroadcastReceiver?正如@hashire在上述链接中所说。