当前位置:编程学习 > wap >>

求助,如何将WEB端的数据,即时同步到Android手机上来.

如题,,具体问题是这样,在WEB端,编辑一条短信,然后利用手机发送出去..哪位大侠有此类经验,忘指教,,我把分全给了... --------------------编程问答-------------------- 应用要具有下载和发送短信功能,
下载
package happly.util;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpDownloader {
public URL url=null;
public String download(String urlstr){
StringBuffer sb=new StringBuffer();
InputStream in = null;
try{
    //得到URL
url = new URL(urlstr);
//得到HTTPURL资源  
HttpURLConnection hc = (HttpURLConnection)url.openConnection();
    //得到输入流
in= hc.getInputStream();
//使用InputStream的anailalble方法得到输入流的字节数
int n=in.available();
//声明一个字节数组
byte[] buf=new byte[n];
while(in.read(buf)!=-1){
//转换字符编码
 String str=new String(buf,"GB2312");
//   String str=new String(buf);
sb.append(str);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally{
try{
in.close();
}
catch(Exception e){
e.printStackTrace();
}
}
//返回字符串并过滤'\r'
return sb.toString().replace("\r\n", "\n");
}
}

发送:
package happly.main;

import happly.main.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SendSms extends Activity {
String str="abd";
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.send_sms);
        Intent intent=getIntent();
         str=intent.getStringExtra("one").toString();
         System.out.println(str);
         final EditText smsNum=(EditText)findViewById(R.id.sms_num_id);
         final EditText smsText=(EditText)findViewById(R.id.sms_text_id);
         smsText.setText(str);
        Button   smsSend=(Button)findViewById(R.id.sms_send_id);
        smsSend.setOnClickListener(new OnClickListener(){

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String phoneNum=smsNum.getText().toString();
//String phoneText=smsText.getText().toString();
       // smsText.setText("abc");
String phoneText=smsText.getText().toString();
if(phoneNum.length()>0&&phoneText.length()>0){
sendSMS(phoneNum,phoneText);
}
else {
Toast.makeText(SendSms.this,R.string.error,Toast.LENGTH_LONG).show();
}
}

private void sendSMS(String phoneNumber, String Massage) {
// TODO Auto-generated method stub
     //PendingIntent pi = PendingIntent.getActivity(SendSms.this,0,new Intent(SendSms.this,SendSms.class),0);
     SmsManager sms = SmsManager.getDefault();
     sms.sendTextMessage(phoneNumber, null,Massage,null,null);
     Toast.makeText(SendSms.this, R.string.succeed,Toast.LENGTH_SHORT).show();
     SendSms.this.finish();
}
});
    }
}
--------------------编程问答-------------------- 没啥经验,手机模拟器web server,处理http协议 --------------------编程问答-------------------- 在自己的android端通过webview获取网页中的短信信息和手机号,然后调用发送短信的方法发送。 --------------------编程问答-------------------- ...2楼的代码..跟我问的没啥关系啊..我意思是,.手机和WEB端,保持一种长连接状态,,,服务器可以随便发送数据给手机端...就是pushlet框架所实现的功能..不过,具体,怎么实现,我自己实在研究不透啊.. --------------------编程问答-------------------- 在手机客户端后台service维持一个与服务器的连接,保持心跳就可以做到与服务器的长连接。还有在2.2以后google提供了一套C2DM(Cloudto Device Messaging)服务可以,从服务器向手机客户端即时推送各种通知消息,但是由于国内的网络环境,这个服务不太可靠,而且只能用于2.2及以后的系统。
补充:移动开发 ,  Android
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,