java 读取和显示传入的消息文本android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11306150/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 04:36:34  来源:igfitidea点击:

Read and display incoming message text android

javaandroidsms

提问by nilkash

I am new to android development.I am developing small android application. In my application I want to retrieve newly coming sms and display this message to user. My code looks like

我是 android 开发的新手。我正在开发小型 android 应用程序。在我的应用程序中,我想检索新收到的短信并向用户显示此消息。我的代码看起来像

// HellowordActivity.java
package com.example.helloword;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

public class HellowordActivity extends BroadcastReceiver
{
    public void onReceive(Context context, Intent intent)
    {
    Bundle myBundle = intent.getExtras();
    SmsMessage [] messages = null;
    String strMessage = "";

    if (myBundle != null)
    {
        Object [] pdus = (Object[]) myBundle.get("pdus");
        messages = new SmsMessage[pdus.length];

        for (int i = 0; i < messages.length; i++)
        {
            messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            strMessage += "SMS From: " + messages[i].getOriginatingAddress();
            strMessage += " : ";
            strMessage += messages[i].getMessageBody().toString();
            strMessage += "\n";
        }
         // Toast.makeText(context, strMessage, Toast.LENGTH_SHORT).show();

        Intent _intent = new Intent(context, PopupActivity.class);
        _intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        _intent.putExtra("strMessage", strMessage);
        startActivity(_intent);
       }
    }

  }  

I added receiver and permission in Android Manifest.xml

我在 Android Manifest.xml 中添加了接收器和权限

    <uses-permission android:name="android.permission.RECEIVE_SMS"/>

      <receiver android:name=".HellowordActivity" >
                <intent-filter > 
                <action android:name="android.provider.Telephony.SMS_RECEIVED"/> 
                </intent-filter> 
        </receiver> 
<activity android:name=".PopupActivity" android:launchMode="singleTop" />

I am not doing any thing in layout part.What I want as an output when new message will come; message text display to user with simple popup. Need Help.. Thank you...

我在布局部分没有做任何事情。当新消息到来时我想要什么作为输出;通过简单的弹出窗口向用户显示消息文本。需要帮助...谢谢...

回答by K_Anas

Try this it works for me you will get a toast shown to you with the content of the message received:

试试这个它对我有用,你会看到一个带有收到消息内容的祝酒词:

package com.example.a;
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 SMSBroadcastReceiver extends BroadcastReceiver {

            private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
            private static final String TAG = "SMSBroadcastReceiver";

            @Override
            public void onReceive(Context context, Intent intent) {
                 Log.i(TAG, "Intent recieved: " + intent.getAction());

                    if (intent.getAction().equals(SMS_RECEIVED)) {
                        Bundle bundle = intent.getExtras();
                        if (bundle != null) {
                            Object[] pdus = (Object[])bundle.get("pdus");
                            final SmsMessage[] messages = new SmsMessage[pdus.length];
                            for (int i = 0; i < pdus.length; i++) {
                                messages[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
                            }
                            if (messages.length > -1) {
                                Toast.makeText(context, "Message recieved: " + messages[0].getMessageBody(), 7000).show();
                            }
                        }
                    }
               }
        }

The AndroidManifest.xml

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.a"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.RECEIVE_SMS"/>
    <uses-permission android:name="android.permission.HARDWARE_TEST"/>


    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <receiver android:name=".SMSBroadcastReceiver" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" >
                </action>
            </intent-filter>
        </receiver>
    </application>

</manifest>

Use DDMS to send sms to your emulator via Telnet

使用 DDMS 通过 Telnet 向您的模拟器发送短信

回答by ρяσ?ρ?я K

if you want to show an popup when SMS is Recived then you will need to Create an Activity with android:launchMode="singleTop"as:

如果要在收到 SMS 时显示弹出窗口,则需要使用以下内容创建活动android:launchMode="singleTop"

In manifast declare Activity as:

在清单中声明 Activity 为:

<activity android:name=".PopupActivity" android:launchMode="singleTop" />

From HellowordActivity BroadcastReceiver start Activity as:

从 HellowordActivity BroadcastReceiver 启动 Activity 为:

Intent intent = new Intent(context, PopupActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent .putExtra("strMessage", strMessage);
context.startActivity(intent);

And in your PopupActivity.class:

而在你的PopupActivity.class

public class PopupActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // The activity is being created.
        Intent intent= getIntent();//get message here
        String strMessage = intent.getStringExtra("strMessage");
        //NOW YOU CAN SHOW THIS MESSAGE IN POPUP
    }
    @Override
    protected void onStop() {
        super.onStop();
        this.finish();
        // The activity is no longer visible (it is now "stopped")
    }

You can also checkout these Smspopup apps source code:

您还可以查看这些 Smspopup 应用程序源代码:

android-smspopup

android-smspopup

sms-enhancer

短信增强器