基本信息
源码名称:android 消息推送系统例子源码(jpush)
源码大小:4.23M
文件格式:.rar
开发语言:Java
更新时间:2016-04-26
友情提示:(无需注册或充值,赞助后即可获取资源下载链接)
嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元×
微信扫码支付:2 元
×
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
源码介绍
内含代码和论文
内含代码和论文
package com.example.jpushdemo; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import cn.jpush.android.api.InstrumentedActivity; import cn.jpush.android.api.JPushInterface; import com.example.push.MessageActivity; import com.example.push.R; public class MainActivity extends InstrumentedActivity implements OnClickListener{ private Button mInit; private Button mSetting; private Button mStopPush; private Button mResumePush; private Button mMessageList; private EditText msgText; private String username; public static boolean isForeground = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Bundle name=getIntent().getExtras(); username=name.getString("username"); initView(); registerMessageReceiver(); // used for receive msg } private void initView(){ TextView mImei = (TextView) findViewById(R.id.tv_imei); String udid = ExampleUtil.getImei(getApplicationContext(), ""); if (null != udid) mImei.setText("IMEI: " udid); TextView mAppKey = (TextView) findViewById(R.id.tv_appkey); String appKey = ExampleUtil.getAppKey(getApplicationContext()); if (null == appKey) appKey = "AppKey异常"; mAppKey.setText("AppKey: " appKey); String packageName = getPackageName(); TextView mPackage = (TextView) findViewById(R.id.tv_package); mPackage.setText("PackageName: " packageName); String deviceId = ExampleUtil.getDeviceId(getApplicationContext()); TextView mDeviceId = (TextView) findViewById(R.id.tv_device_id); mDeviceId.setText("deviceId:" deviceId); String versionName = ExampleUtil.GetVersion(getApplicationContext()); TextView mVersion = (TextView) findViewById(R.id.tv_version); mVersion.setText("Version: " versionName); mInit = (Button)findViewById(R.id.init); mInit.setOnClickListener(this); mStopPush = (Button)findViewById(R.id.stopPush); mStopPush.setOnClickListener(this); mResumePush = (Button)findViewById(R.id.resumePush); mResumePush.setOnClickListener(this); mMessageList=(Button)findViewById(R.id.messageList); mMessageList.setOnClickListener(this); mSetting = (Button)findViewById(R.id.setting); mSetting.setOnClickListener(this); msgText = (EditText)findViewById(R.id.msg_rec); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.init: init(); break; case R.id.setting: Intent intent = new Intent(MainActivity.this, PushSetActivity.class); startActivity(intent); break; case R.id.stopPush: JPushInterface.stopPush(getApplicationContext()); break; case R.id.resumePush: JPushInterface.resumePush(getApplicationContext()); break; case R.id.messageList:////////////////// Bundle bundle = new Bundle(); bundle.putString("username",username); Intent intent1 = new Intent(MainActivity.this, MessageActivity.class); intent1.putExtras(bundle); startActivity(intent1); break; } } // 初始化 JPush。如果已经初始化,但没有登录成功,则执行重新登录。 private void init(){ JPushInterface.init(getApplicationContext()); } @Override protected void onResume() { isForeground = true; super.onResume(); } @Override protected void onPause() { isForeground = false; super.onPause(); } @Override protected void onDestroy() { unregisterReceiver(mMessageReceiver); super.onDestroy(); } //for receive customer msg from jpush server private MessageReceiver mMessageReceiver; public static final String MESSAGE_RECEIVED_ACTION = "com.example.jpushdemo.MESSAGE_RECEIVED_ACTION"; public static final String KEY_TITLE = "title"; public static final String KEY_MESSAGE = "message"; public static final String KEY_EXTRAS = "extras"; public void registerMessageReceiver() { mMessageReceiver = new MessageReceiver(); IntentFilter filter = new IntentFilter(); filter.setPriority(IntentFilter.SYSTEM_HIGH_PRIORITY); filter.addAction(MESSAGE_RECEIVED_ACTION); registerReceiver(mMessageReceiver, filter); } public class MessageReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { if (MESSAGE_RECEIVED_ACTION.equals(intent.getAction())) { String messge = intent.getStringExtra(KEY_MESSAGE); String extras = intent.getStringExtra(KEY_EXTRAS); StringBuilder showMsg = new StringBuilder(); showMsg.append(KEY_MESSAGE " : " messge "\n"); if (!ExampleUtil.isEmpty(extras)) { showMsg.append(KEY_EXTRAS " : " extras "\n"); } setCostomMsg(showMsg.toString()); } } } private void setCostomMsg(String msg){ if (null != msgText) { msgText.setText(msg); msgText.setVisibility(android.view.View.VISIBLE); } } }