我的目标是创建一个短信服务器,能够通过发送短信向请求的用户发送短信...我创建了第一个广播接收器来接收短信并获得发送者号码和消息体。 与那个身体(滚动否)另一个活动与该卷没有导致该人的标记.. 起初我将消息体从broadcastreceiver传递给活动.. 这是我的BroadcastReceiver代码:
public class IncomingSms extends BroadcastReceiver{
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
if(arg1.getAction().equals("android.provider.Telephony.SMS_RECEIVED")) {
Bundle bundle = arg1.getExtras();
SmsMessage[] msgs = null;
String msg_from;
String msgBody;
if(bundle!=null) {
try {
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]);
msg_from = msgs[i].getOriginatingAddress();
msgBody = msgs[i].getMessageBody();
Toast.makeText(arg0, "SenderNum :" + msg_from + "msg :" + msgBody, Toast.LENGTH_LONG).show();
Intent in = new Intent("SmsMessage.intent.MAIN");
in.putExtra("get_msg", msgBody);
arg0.sendBroadcast(in);
}
msg_from = msgs[0].getOriginatingAddress();
String msg = arg1.getStringExtra("string");
Toast.makeText(arg0, "Sms sent", Toast.LENGTH_LONG).show();
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(msg_from, null, msg, null, null);
} catch(Exception e) {
Log.d("Exception caught", e.getMessage());
}
}
}
}
}`
之后,将该消息体与现有的roll no's数据库进行比较,如果找到则将此字符串传递给broadcastreceiver,以便向用户发送短信... 这是我的活动代码:
public class MyApp extends Activity implements OnClickListener
{
EditText editRollno,editName,editMarks;
Button btnAdd,btnDelete,btnModify,btnView,btnViewAll,btnShowInfo;
SQLiteDatabase db;
String s;
String s1,s2,s3;
String str,str1;
private BroadcastReceiver bd;
IncomingSms is = new IncomingSms();
String pNumber;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
registerReceiver(is,new IntentFilter("MyReceiver"));
Intent i = new Intent("MyReceiver");
i.putExtra("string", s3);
sendBroadcast(i);
editRollno=(EditText)findViewById(R.id.editRollno);
editName=(EditText)findViewById(R.id.editName);
editMarks=(EditText)findViewById(R.id.editMarks);
btnAdd=(Button)findViewById(R.id.btnAdd);
btnDelete=(Button)findViewById(R.id.btnDelete);
btnModify=(Button)findViewById(R.id.btnModify);
btnView=(Button)findViewById(R.id.btnView);
btnViewAll=(Button)findViewById(R.id.btnViewAll);
btnShowInfo=(Button)findViewById(R.id.btnShowInfo);
btnAdd.setOnClickListener(this);
btnDelete.setOnClickListener(this);
btnModify.setOnClickListener(this);
btnView.setOnClickListener(this);
btnViewAll.setOnClickListener(this);
btnShowInfo.setOnClickListener(this);
db=openOrCreateDatabase("StudentDB", Context.MODE_WORLD_READABLE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS student(rollno VARCHAR,name VARCHAR,marks VARCHAR);");
}
public void onClick(View view)
{
if(view==btnAdd)
{
if(editRollno.getText().toString().trim().length()==0||
editName.getText().toString().trim().length()==0||
editMarks.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter all values");
return;
}
db.execSQL("INSERT INTO student VALUES('"+editRollno.getText()+"','"+editName.getText()+
"','"+editMarks.getText()+"');");
showMessage("Success", "Record added");
clearText();
}
if(view==btnDelete)
{
if(editRollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+editRollno.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("DELETE FROM student WHERE rollno='"+editRollno.getText()+"'");
showMessage("Success", "Record Deleted");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
}
if(view==btnModify)
{
if(editRollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+editRollno.getText()+"'", null);
if(c.moveToFirst())
{
db.execSQL("UPDATE student SET name='"+editName.getText()+"',marks='"+editMarks.getText()+
"' WHERE rollno='"+editRollno.getText()+"'");
showMessage("Success", "Record Modified");
}
else
{
showMessage("Error", "Invalid Rollno");
}
clearText();
}
if(view==btnView)
{
if(editRollno.getText().toString().trim().length()==0)
{
showMessage("Error", "Please enter Rollno");
return;
}
Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+editRollno.getText()+"'", null);
if(c.moveToFirst())
{
editName.setText(c.getString(1));
editMarks.setText(c.getString(2));
}
else
{
showMessage("Error", "Invalid Rollno");
clearText();
}
}
if(view==btnViewAll)
{
Cursor c=db.rawQuery("SELECT * FROM student", null);
if(c.getCount()==0)
{
showMessage("Error", "No records found");
return;
}
StringBuffer buffer=new StringBuffer();
while(c.moveToNext())
{
buffer.append("Rollno: "+c.getString(0)+"\n");
buffer.append("Name: "+c.getString(1)+"\n");
buffer.append("Marks: "+c.getString(2)+"\n\n");
}
showMessage("Student Details", buffer.toString());
}
if(view==btnShowInfo)
{
showMessage("Student Management Application", "Developed By Azim");
}
}
public void showMessage(String title,String message)
{
Builder builder=new Builder(this);
builder.setCancelable(true);
builder.setTitle(title);
builder.setMessage(message);
builder.show();
}
public void clearText()
{
editRollno.setText("");
editName.setText("");
editMarks.setText("");
editRollno.requestFocus();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
IntentFilter ifl = new IntentFilter("SmsMessage.intent.MAIN");
bd = new BroadcastReceiver() {
@Override
public void onReceive(Context arg0, Intent arg1) {
// TODO Auto-generated method stub
String msg = arg1.getStringExtra("get_msg");
Cursor c=db.rawQuery("SELECT * FROM student WHERE rollno='"+ msg +"'", null);
if(c.moveToFirst())
{
s1 = c.getString(1);
s2 = c.getString(2);
s3 = s1.concat(s2);
}
else
{
}
}
};
this.registerReceiver(bd, ifl);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
this.unregisterReceiver(this.bd);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
unregisterReceiver(is);
super.onDestroy();
}
}
执行此操作后,它接收到短信但没有发送短信但它发送短信发送短信...我无法理解..plzzz帮助我...谢谢提前..
答案 0 :(得分:0)
试试这个:
msg_from = msgs[0].getOriginatingAddress();
String msg = arg1.getStringExtra("string");
PendingIntent sentPI = PendingIntent.getBroadcast(this, 0,new Intent(SENT), 0); // <-- add this
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(msg_from, null, msg, sentPI, null); //<-- Add this
Toast.makeText(arg0, "Sms sent", Toast.LENGTH_LONG).show(); //<-- move it here