求教一个简单的服务器向手机推送信息的功能。 实现一个简单的服务器向手机推送信息的功能,hello world即可。求大侠给点代码学习学习。感激不尽! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 server推送client,如果不是很频繁,客户端轮循查吧 《Android应用开发揭秘》第八章中有例子,网上可以搜到源码 package com.yarin.android.Examples_08_03;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;public class Activity01 extends Activity{ private final String DEBUG_TAG = "Activity02"; private TextView mTextView; private Button mButton; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mTextView = (TextView)this.findViewById(R.id.TextView01); mButton = (Button)this.findViewById(R.id.Button01); mButton.setOnClickListener(new Button.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub refresh(); } }); //开启线程 new Thread(mRunnable).start(); } //刷新网页显示 private void refresh() { String httpUrl = "http://192.168.1.110:8080/date.jsp"; String resultData = ""; URL url = null; try { // 构造一个URL对象 url = new URL(httpUrl); } catch (MalformedURLException e) { Log.e(DEBUG_TAG, "MalformedURLException"); } if (url != null) { try { // 使用HttpURLConnection打开连接 HttpURLConnection urlConn = (HttpURLConnection) url.openConnection(); // 得到读取的内容(流) InputStreamReader in = new InputStreamReader(urlConn.getInputStream()); // 为输出创建BufferedReader BufferedReader buffer = new BufferedReader(in); String inputLine = null; // 使用循环来读取获得的数据 while (((inputLine = buffer.readLine()) != null)) { // 我们在每一行后面加上一个"\n"来换行 resultData += inputLine + "\n"; } // 关闭InputStreamReader in.close(); // 关闭http连接 urlConn.disconnect(); // 设置显示取得的内容 if (resultData != null) { mTextView.setText(resultData); } else { mTextView.setText("读取的内容为NULL"); } } catch (IOException e) { Log.e(DEBUG_TAG, "IOException"); } } else { Log.e(DEBUG_TAG, "Url NULL"); } } private Runnable mRunnable = new Runnable() { public void run() { while (true) { try { Thread.sleep(5 * 1000); //发送消息 mHandler.sendMessage(mHandler.obtainMessage()); } catch (InterruptedException e) { // TODO Auto-generated catch block Log.e(DEBUG_TAG, e.toString()); } } } }; Handler mHandler = new Handler() { public void handleMessage(Message msg) { super.handleMessage(msg); //刷新 refresh(); } };}每隔5秒要服务器拿一次数据 我觉得这种方式还是客户端每隔一段时间去服务端看是否有新的内容,而不是由服务器主动向客户端发送消息。我觉得用Socket可能比这还好些。 问题是socket行不行?如何获取手机ip?获取到之后开个端口,用http连接一次server端,server因此获得其ip地址,然后在server端用socket连接? 怎么在图片上加文字? 如何修改系统时间。 为什么有的时候屏幕不旋转 [Android Robotium]Browser的URL输入框如何输入字符? 请问Android 上层应用里的Java程序的消息怎么打印到串口里? Android 开发最常见问题讨论——探讨解决方法 android中资源的Uri(通过浏览器浏览资源中的页面) iphone 的app store网址是多少啊? 跨类操作button,求解 非Activity类里面调用Activity里面的方法 android源码怎么看?想学习下framework,但是不知道怎么去阅读代码 疑问:windows phone7开发用的什么语言?
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class Activity01 extends Activity
{
private final String DEBUG_TAG = "Activity02";
private TextView mTextView;
private Button mButton;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView)this.findViewById(R.id.TextView01);
mButton = (Button)this.findViewById(R.id.Button01);
mButton.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
refresh();
}
});
//开启线程
new Thread(mRunnable).start();
}
//刷新网页显示
private void refresh()
{
String httpUrl = "http://192.168.1.110:8080/date.jsp";
String resultData = "";
URL url = null;
try
{
// 构造一个URL对象
url = new URL(httpUrl);
}
catch (MalformedURLException e)
{
Log.e(DEBUG_TAG, "MalformedURLException");
}
if (url != null)
{
try
{
// 使用HttpURLConnection打开连接
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// 得到读取的内容(流)
InputStreamReader in = new InputStreamReader(urlConn.getInputStream());
// 为输出创建BufferedReader
BufferedReader buffer = new BufferedReader(in);
String inputLine = null;
// 使用循环来读取获得的数据
while (((inputLine = buffer.readLine()) != null))
{
// 我们在每一行后面加上一个"\n"来换行
resultData += inputLine + "\n";
}
// 关闭InputStreamReader
in.close();
// 关闭http连接
urlConn.disconnect();
// 设置显示取得的内容
if (resultData != null)
{
mTextView.setText(resultData);
}
else
{
mTextView.setText("读取的内容为NULL");
}
}
catch (IOException e)
{
Log.e(DEBUG_TAG, "IOException");
}
}
else
{
Log.e(DEBUG_TAG, "Url NULL");
}
}
private Runnable mRunnable = new Runnable()
{
public void run()
{
while (true)
{
try
{
Thread.sleep(5 * 1000);
//发送消息
mHandler.sendMessage(mHandler.obtainMessage()); } catch (InterruptedException e)
{
// TODO Auto-generated catch block
Log.e(DEBUG_TAG, e.toString());
}
}
}
};
Handler mHandler = new Handler()
{
public void handleMessage(Message msg)
{
super.handleMessage(msg);
//刷新
refresh();
}
};
}
每隔5秒要服务器拿一次数据
我觉得用Socket可能比这还好些。