android消息推送

网上搜索到如下类似文章,经调试可行后改成自己的代码再发上来记录一下

独立进程 
无论程序是否正在运行,我们都要能通知到客户,我们需要一个独立进程的后台服务。 
我们需要一个独立进程的后台服务。 
在androidmanifest.xml中注册service时,有一个android:process属性,如果这个属性以"."开头,则为此服务开启一个 
全局的独立进程,如果以":"开头则为此服务开启一个为此应用私有的独立进程。举个具体的例子吧,我们新建了一个 
application,创建了主进程com.example.exerciseproc,那么: 

<!--下面会创建一个全局的com.example.exerciseproc的独立进程--> 

 <service android:name="com.example.exerciseproc.MessageService" android:label="消息推送" android:process=".message"/>

<!--或者--> 
<!--下面会创建一个应用私有的com.cnblogs.tianxia:message的独立进程--> 

 <service android:name="com.example.exerciseproc.MessageService" android:label="消息推送" android:process=":message"/>

通知用户和点击查看 

android消息推送
package com.example.exerciseproc;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.ServerSocket;
import java.net.Socket;

import android.R;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

public class MessageService extends Service{
    private Intent msgintent=null;
    private PendingIntent msgpendingintent=null;
    private int messageNotificationID=1000;
    private Notification messageNotification=null;
    private NotificationManager msgNotificationManager=null;
    private MessageThread msgThread=null;
    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

@Override
public void onDestroy()
{
System.exit(0);
super.onDestroy();
}


    @Override
    public int onStartCommand(Intent intent,int flags,int startId)
    {
        messageNotification=new Notification();
        messageNotification.icon=R.drawable.star_big_on;//推送的图标
        messageNotification.tickerText="新消息";//推送标题
        messageNotification.defaults=Notification.DEFAULT_SOUND;
     messageNotification.flags=Notification.FLAG_AUTO_CANCEL;//点击后自动关闭推送 msgNotificationManager
=(NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); msgintent=new Intent(this,TestProc.class);//点击推送需要跳转到的窗体 msgpendingintent=PendingIntent.getActivity(this, 0, msgintent, 0); msgThread=new MessageThread(); msgThread.isRunning=true; msgThread.start(); return super.onStartCommand(intent, flags, startId); } class MessageThread extends Thread{ public boolean isRunning=true; public void run() { try { Log.d("消息服务", "初始化监听,端口:51230"); ServerSocket socket=new ServerSocket(51230); while(isRunning) { Log.d("消息服务", "等待接收数据"); Thread.sleep(1000); Socket ReceiveSc=socket.accept(); Log.d("消息服务", "开始接收数据"); try{ BufferedReader bfr=new BufferedReader(new InputStreamReader(ReceiveSc.getInputStream(),"UTF-8")); final String msg=bfr.readLine(); Log.d("消息服务内容", msg); messageNotification.setLatestEventInfo(MessageService.this, ReceiveSc.getInetAddress().toString(), msg, msgpendingintent); msgNotificationManager.notify(messageNotificationID,messageNotification); messageNotificationID++; } catch(Exception e) { Log.d("服务器", e.getMessage()); } finally { ReceiveSc.close(); } } } catch(Exception e) { System.out.println(e.getMessage()); } } } }
android消息推送

在Activity中启动服务

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                 
          Log.d("消息服务", "正在启动消息推送服务");
          startService(new Intent(this,MessageService.class));
    }

 

 

android消息推送,布布扣,bubuko.com

android消息推送

上一篇:Unity3D学习笔记——Android远程真机调试(Unity Remote)


下一篇:iostat查看linux硬盘IO性能