您好我正在开发一个完全基于短信的应用程序。我可以将成功发送到所需的号码,但我的问题是我没有收到发送和传递的通知,即使我使用代码通过意图过滤器和待处理意图获取通知。发送短信和接收通知的代码在扩展广播接收器的类 (SmsHandling.java) 中完成。我的程序代码在这里..
public class SmsHandling extends BroadcastReceiver{
IntentFilter sendFilter,deliveredFilter;
Context mcontext;
String result="got it";
public SmsHandling(Mobile mobile) {
// TODO Auto-generated constructor stub
mcontext=mobile;
}
String sendSms(String num,String messege){
Log.d("inside method", "getting");
String SENT="SMS_SENT";
String DELIVERED="SMS_DELIVERED";
sendFilter=new IntentFilter("SMS_SEND");
deliveredFilter=new IntentFilter("SMS_DELIVERED");
PendingIntent sendPI=PendingIntent.getBroadcast(mcontext, 0, new Intent(SENT), 0);
PendingIntent deliveredPI=PendingIntent.getBroadcast(mcontext, 0, new Intent(DELIVERED), 0);
mcontext.registerReceiver(sendingSms, sendFilter);
mcontext.registerReceiver(deliveredSms, deliveredFilter);
Log.d("inside method 2", "getting 2");
SmsManager sms=SmsManager.getDefault();
sms.sendTextMessage(num, null, messege, null, null);
Log.d("inside method 3", "getting 3");
return result;
}
BroadcastReceiver sendingSms=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(context, "Sms Send", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
Toast.makeText(context, "Generic Errors", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(context, "No Service", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_NULL_PDU:
Toast.makeText(context, "Null pdu", Toast.LENGTH_SHORT).show();
break;
case SmsManager.RESULT_ERROR_RADIO_OFF:
Toast.makeText(context, "Error Radio", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
};
BroadcastReceiver deliveredSms=new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
switch (getResultCode()) {
case Activity.RESULT_OK:
Toast.makeText(context, "Sms Delivered", Toast.LENGTH_SHORT).show();
break;
case Activity.RESULT_CANCELED:
Toast.makeText(context, "Sms Failed", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
};
public void unregisterreciever(){
mcontext.unregisterReceiver(sendingSms);
mcontext.unregisterReceiver(deliveredSms);
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
System.out.print("Recieved : "+intent.getAction());
}
}
我是否需要做更多类似意图过滤器的事情,而不是在清单文件中声明接收者我的清单文件如下所示:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.rrm"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.BROADCAST_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.rrm.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Mobile"
android:label="@string/app_name"></activity>
<activity
android:name=".Dth"
android:label="@string/app_name"></activity>
<activity
android:name=".Datacard"
android:label="@string/app_name"></activity>
<activity
android:name=".Check"
android:label="@string/app_name"></activity>
<activity
android:name=".Register"
android:label="@string/app_name"></activity>
<receiver
android:name=".SmsHandling"></receiver>
</application>
</manifest>
而且我对 Intent 过滤器没有正确的想法,任何人都可以解释为什么在清单文件中使用它。并且还可以帮助我解决上述问题