嗨,我正在开发一个 android SMS 应用程序,我在引用以下链接的活动中使用广播接收器,
我试过这段代码
public class Myapp extends Activity
{
BroadcastReceiver sendBroadcastReceiver= new sentReceiver();
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activtiy_main);
}
public void onClick(View v)
{
if(v.getId()==sendmsg.getId())
{
sendSMS(phoneNumber,message);
}
@Override
protected void onDestroy()
{
super.onDestroy();
try
{
unregisterReceiver(sendBroadcastReceiver);
}
catch (Exception e)
{
e.printStackTrace();
}
}
//unregistering Receiver even in onPause()
private void sendSMS(String phoneNumber, String message)
{
String SENT = "SMS_SENT";
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0, new Intent(SENT), 0);
registerReceiver(sendBroadcastReceiver, new IntentFilter(SENT));
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(phoneNumber, null, message, sentPI, null);
}
class sentReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent arg1)
{
switch (getResultCode())
{
case Activity.RESULT_OK:
Toast.makeText(getBaseContext(), "sms_sent", Toast.LENGTH_SHORT).show();
startActivity(new Intent(Myapp.this, Sm.class));
break;
case SmsManager.RESULT_ERROR_NO_SERVICE:
Toast.makeText(getBaseContext(), "No service",Toast.LENGTH_SHORT).show();
Log.e("No service","");
break;
}
}
}
}
但我收到警告
java.lang.IllegalArgumentException: Receiver not registered: com.example.Myapp$Receiver@40593ab8
不确定我需要如何在清单中注册接收器。请建议。谢谢!