5.Android消息推送机制简单例子

1.首先布局文件xml代码:

 <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.riger.notificationdemo.MainActivity"> <LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hello"
android:id="@+id/textView" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="通知"
android:id="@+id/btnStart" /> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="清除"
android:id="@+id/btnStop" />
</LinearLayout>
</RelativeLayout>

2.主界面代码MainActivity:

 public class MainActivity extends Activity implements View.OnClickListener{

     private Button btnStart;
private Button btnStop;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
} public void initView(){
btnStart = (Button)findViewById(R.id.btnStart);
btnStop = (Button)findViewById(R.id.btnStop);
btnStart.setOnClickListener(this);
btnStop.setOnClickListener(this);
} @Override
public void onClick(View view) {
int id = view.getId();
switch(id){
case R.id.btnStart:
//启动服务
Intent intent = new Intent();
intent.setAction("ymw.MY_SERVICE");
intent.setPackage("com.example.riger.notificationdemo");
startService(intent);
break;
case R.id.btnStop:
Intent i = new Intent();
i.setAction("ymw.MY_SERVICE");
i.setPackage("com.example.riger.notificationdemo");
stopService(i);
break;
default:
break;
} }
}

3.消息推送服务文件NotificationService

 public class NotificationService extends Service{

     private static final String TAG = NotificationService.class.getSimpleName();
//获取消息线程
private MessageThread messageThread = null; //点击查看
private Intent messageIntent = null;
private PendingIntent messagePendingIntent = null; //通知栏消息
private NotificationManager messageNotificationManage = null; @Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
} @Override
public int onStartCommand(Intent intent, int flags, int startId) {
// 初始化
messageNotificationManage = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); messageIntent = new Intent(this,MainActivity.class);
messagePendingIntent = PendingIntent.getActivity(this,0,messageIntent,0); // 开启线程
messageThread = new MessageThread();
messageThread.isRunning = true;
messageThread.start(); return super.onStartCommand(intent,flags,startId);
} /**
* 从服务器获取信息
*/
class MessageThread extends Thread{
public boolean isRunning = false;
public void run() {
try{
Log.v(TAG,"111111111");
// 间隔时间
Thread.sleep(1000);
// 获取服务器消息
String serverMessage = getServerMessage();
if (serverMessage != null && !"".equals(serverMessage)) {
Log.v(TAG,"22222222");
// 更新通知栏
Notification.Builder builder = new Notification.Builder(NotificationService.this);
builder.setSmallIcon(R.mipmap.ic_launcher);
builder.setTicker("显示第一个通知");
builder.setContentTitle("第一个通知");
builder.setContentText("Hello World!");
builder.setWhen(System.currentTimeMillis()); //发送时间
builder.setDefaults(Notification.DEFAULT_ALL);
Notification notification = builder.build();
messageNotificationManage.notify(123, notification);
Log.v(TAG,"33333333");
} }catch (InterruptedException e) {
e.printStackTrace();
}
}
} @Override
public void onDestroy(){
messageThread.isRunning = false;
super.onDestroy();
} /**
* 模拟发送信息
*/
public String getServerMessage() {
return "HELLO WORLD!";
} }

最后记得配置下Service:

 <service android:name=".NotificationService">
<intent-filter>
<action android:name="ymw.MY_SERVICE"/>
</intent-filter>
</service>

运行效果:

5.Android消息推送机制简单例子    5.Android消息推送机制简单例子  5.Android消息推送机制简单例子

上一篇:(6CBIR模拟问题)自己动手,编写神经网络程序,解决Mnist问题,并网络化部署


下一篇:现代IM系统中消息推送和存储架构的实现