我有一点问题。我无法让我的广播接收器显示任何Toast消息。也许我无法以某种方式触发它,但我的发件人工作。我想知道它是否会触发。
主要课程
public class Main_Activity extends Activity {
Button buttonSend;
EditText textPhoneNo;
EditText textSMS;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button buttons = (Button) findViewById(R.id.gett);
buttonSend = (Button) findViewById(R.id.buttonSend);
textPhoneNo = (EditText) findViewById(R.id.editTextPhoneNo);
textSMS = (EditText) findViewById(R.id.editTextSMS);
EditText edt = (EditText)findViewById(R.id.reciving);
buttons.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
// EditText edt = (EditText)findViewById(R.id.reciving);
// SmsReceiver obj = new SmsReceiver();
// String test = obj.number2;
// edt.setText("you stupid result "+test);
}
});
SmsReceiver obj = new SmsReceiver();
// String test = obj.number2;
// edt.setText("you stupid result "+test);
buttonSend.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String phoneNo = textPhoneNo.getText().toString();
String sms = textSMS.getText().toString();
try{
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, sms, null, null);
}catch (Exception e)
{
System.out.println("Not send");
}
}
}) ;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
SmsReceiver类:
public class SmsReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
Mainifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dom.example.sms_exp"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Main_Activity"
android:label="@string/title_activity_main_" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".SmsReceiver" >
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity
android:name=".SmsReceiver"
android:label="@string/title_activity_send_activity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
请帮助,我明天有一个项目要提交。
答案 0 :(得分:0)
如果您希望您的应用程序接收SMS广播,那么您必须在manifest中包含以下权限
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
并将您的SmsReceiver代码更改为:
public class SmsReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals("android.provider.Telephony.SMS_RECEIVED"))
{
//put your code here....
}
else{
// do something here...
}
}
}