安卓开发日记3——IP推送器
服务器参考工程:点击打开链接 注意将clientid改成你自己的clientid
客户端参考工程:点击打开链接
1.引子
2. 开发步骤
package com.tanglikang.ipGiver; import android.os.Bundle; import com.igexin.sdk.Consts; //这两个包需要自己导入 import com.igexin.slavesdk.MessageManager; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.util.Log; import android.view.Menu; import android.widget.TextView; public class MainActivity extends Activity { private static final String ACTIONNAME_STRING="com.igexin.sdk.action.0a6shecpQs9ae01bYTQ9o1"; //这是appid private TextView IP ; private String data="127.0.0.1"; private SharedPreferences sp; private Editor editor; private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); Log.d("GexinSdkDemo", "onReceive() action=" + bundle.getInt("action")); switch (bundle.getInt(Consts.CMD_ACTION)) { case Consts.GET_MSG_DATA: // 获取透传(payload)数据 byte[] payload = bundle.getByteArray("payload"); if (payload != null) { data = new String(payload); Log.d("GexinSdkDemo", "Got Payload:" + data); editor.putString("IP", data); //将透传消息内容保存到sharedPreference,以便于下次打开时能够查看 editor.commit();//别忘了commit IP.setText(data);//将获得的ip地址在显示在activity中 } break; case Consts.GET_CLIENTID: // 获取ClientID(CID) String cid = bundle.getString("clientid"); Log.d("GexinSdkDemo", "Got ClientID:" + cid); // TODO: /* 第三方应用需要将ClientID上传到第三方服务器,并且将当前用户帐号和ClientID进行关联,以便以后通过用户帐号查找ClientID进行消息推送 有些情况下ClientID可能会发生变化,为保证获取最新的ClientID,请应用程序在每次获取ClientID广播后,都能进行一次关联绑定 */ break; default: break; } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); sp = this.getSharedPreferences("SP", MODE_WORLD_READABLE); //获得sharedPreferences对象。 editor = sp.edit(); //获得sharedPreferences编辑对象 IP = (TextView) findViewById(R.id.tv); MessageManager.getInstance().initialize(this.getApplicationContext()); //初始化sdk registerBroadcastReceiver(); //调用注册broadcast receiver类函数 data = sp.getString("IP", "127.0.0.1"); //获得sharedPreferences中保存的ip IP.setText(data); } void registerBroadcastReceiver(){ //对broadcast进行注册 IntentFilter intentFilter = new IntentFilter(); // intentFilter.addAction(ACTIONNAME_STRING); //消息过滤设置消息类型 registerReceiver(broadcastReceiver, intentFilter); //注册receiver,并添加消息过滤器 } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
import java.net.InetAddress; import com.gexin.rp.*; //下面这些包需要自己导入 import com.gexin.rp.sdk.base.IPushResult; import com.gexin.rp.sdk.base.impl.SingleMessage; import com.gexin.rp.sdk.base.impl.Target; import com.gexin.rp.sdk.http.IGtPush; import com.gexin.rp.sdk.template.TransmissionTemplate; public class ipGiver { private static final String APPID = "0a6shecpQs9ae01bYTQ9o1"; private static final String APPKEY = "rGjehsZMuq5V46utMwp6x3"; private static final String MASTERSECRET = "ICMQ5vGvFc5URALXIj35v7"; private static final String CLIENTID = "你的clientid";//获得方式可以参考技术文档,我的获取方式,通过真机调试在logcat中查看 private static final String API = "http://sdk.open.api.igexin.com/apiex.htm"; public static void main(String [] args){ while(true){ pushIp(); //向手机推送IP try { Thread.sleep(1000*60*5); //睡眠5分钟再次推送 } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } public static void pushIp(){ IGtPush iGtPush = new IGtPush(API, APPKEY, MASTERSECRET); //推送对象 String ip=""; try { InetAddress address = InetAddress.getLocalHost(); //获得本机InetAddress 对象 ip = address.getHostAddress(); //通过InetAddress对象获得本机ip } catch (Exception e) { // TODO: handle exception } try { SingleMessage message = new SingleMessage(); //单推送消息对象,只想指定手机推送消息 TransmissionTemplate template = new TransmissionTemplate(); //推送模板 template.setAppId(APPID); template.setAppkey(APPKEY); template.setTransmissionContent(ip); //设置推送内容 template.setTransmissionType(2); //设置类型: 1:接受消息后自动运行应用 2: 接受消息等待用户运行应用 message.setData(template); //将模板导入message对象 message.setOffline(true); //设置是否用户离线时在服务器保存 message.setOfflineExpireTime(1000*60*60); //设置保存时间 Target target = new Target(); //设置Target对象,定义了向什么地方发送 target.setAppId(APPID); target.setClientId(CLIENTID); IPushResult iResult = iGtPush.pushMessageToSingle(message, target); //单推送消息,并获得推送结果 System.out.println(iResult.toString()); } catch (Exception e) { // TODO: handle exception } } }
步骤三:
F: #你jar包所放路径 IpGiverServer.jar #你jar包的名字
然后再写一个reg文件实现bat文件的开机自启动注册
Windows Registry Editor Version 5.00 [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run] "IpGiverServer"="C:\\autoRun\\test.bat" # bat文件的路径,注意要用双斜杠在开始运行中regedit,然后倒入上面的reg文件。
3.总结
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。