我像以前一样需要你的帮助。我的应用程序的一部分是关于在Android中发送和接收短信。我做了很多研究,尤其是在此论坛上的帖子。但我没有得到我正在寻找的答案。我能够在接收Android手机发送和接收短信。重要的是,我想处理收到的短信,将其显示为TOAST。它仅通过通知进入收件箱,但没有TOAST。同时,我正在接收我的broadcastReceiver类中的拨出电话号码,我想知道将SMS作为TOAST处理的规范。请帮帮我!
这是我的Receiver类,用于将SMS处理视为TOAST。真的不是我的:
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class Receiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
Bundle bundle = intent.getExtras();
String recMsgString = "";
String fromAddress = "";
SmsMessage recMsg = null;
byte[] data = null;
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
for (int i = 0; i < pdus.length; i++)
{
recMsg = SmsMessage.createFromPdu((byte[]) pdus[i]);
try
{
data = recMsg.getUserData();
}
catch (Exception e)
{
}
if (data != null)
{
for (int index = 0; index < data.length; ++index)
{
recMsgString += Character.toString((char) data[index]);
}
}
fromAddress = recMsg.getOriginatingAddress();
}
}
Intent result = new Intent(context, ReceiveActivity.class);
result.putExtra(SMSReceive.MESSAGE, "Received SMS from " + fromAddress + ": " + recMsgString);
result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(result);
}
}
ReceiverActivity.java
public class ReceiverActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = getIntent();
String message = intent.getStringExtra(MESSAGE);
Log.i("This is the message", message);
if (message != null)
{
Toast.makeText(getApplicationContext(),message, Toast.LENGTH_LONG).show();
}
}
}
答案 0 :(得分:0)
试试这样:
/*
Intent result = new Intent(context, ReceiveActivity.class);
result.putExtra(SMSReceive.MESSAGE, "Received SMS from " + fromAddress + ": " + recMsgString);
result.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(result);
*/
Toast.makeText(context, recMsgString, Toast.LENGTH_SHORT).show();