1
我是android平臺的新手,我希望創建一個沒有終端設備和協調器的應用程序,他們將通過短信進行通信。在後臺運行並獲得即時更新的Android SMS服務
我已經在j2me中建立它,但現在在android我想做一個服務,在後臺接收短信和不間斷地更新數據庫。我已經寫了一些代碼,工作在簡單的類,但服務很好我怎樣才能將其綁定>
public class SMSReceiver extends BroadcastReceiver {
private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
private SMSSync sync;
private Context context;
public SMSReceiver(Context ctx){
sync = new SMSSync(ctx);
this.context = ctx;
}
@Override
public void onReceive(Context context,final Intent intents){
if (intents.getAction().equals(SMS_RECEIVED)) {
new Thread(){
Context context;
Thread Set(Context ctx){
this.context=ctx;
return this;
}
public void run(){
try{
SmsManager sms = SmsManager.getDefault();
Bundle bundle = intents.getExtras();
/*String uriContent =intent.getDataString();
String[] str = uriContent.split(":");
String strPort = str[str.length-1];
short Port = Short.valueOf(strPort, 16);*/
//if(Port==5000){*/
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
SmsMessage[] messages = new SmsMessage[pdus.length];
for (int i = 0; i < pdus.length; i++)
messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
String msg=null;
String address = null;
for (SmsMessage message : messages) {
msg = message.getMessageBody();
address = message.getOriginatingAddress();
}
////////////sending to SMSSync class//////////////
MainThread th = new MainThread(sync,msg);
try{
th.thread.join();
}catch(Exception e){
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
}
if(msg.substring(3, 4).equals("2"))
ConstantClass.isAuditrequestSend = true;
// sync.smsProcess(msg);
//Toast.makeText(context, "Message Received Successfully", Toast.LENGTH_SHORT).show();
}
// }
////////////////after receiving the sms opening the Main Screen./////////////////////
Intent intent = new Intent(context,ZigbeeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
}catch(Exception e){
Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
}.Set(context).start();
}
}
//}
}