首页 新闻 会员 周边

mono for android的消息推送

0
悬赏园豆:40 [已关闭问题] 关闭于 2013-09-09 12:02

情况很急,望各位解答下!再次先谢过了!

如何用mono for android实现消息推送,要实现的功能就是每隔10分钟从服务器段获取下数据,然后显示在Android的状态栏中,信息要叠加,不能覆盖!这是我写的代码如下,可是不行:

定义的服务和线程:

 1  public class MessageService : Service
 2     {
 3         //获取消息线程
 4         private MessageThread messageThread = null;
 5         //点击查看
 6         private Intent messageIntent = null;
 7         private PendingIntent messagePendingIntent = null;
 8 
 9         //通知栏消息
10         //private int messageNotificationID = 1000;
11         private Notification messageNotification = null;
12         private NotificationManager messageNotificatioManager = null;
13 
14         public override IBinder OnBind(Intent intent)
15         {
16             return null;
17         }
18 
19         public override StartCommandResult OnStartCommand(Intent intent, StartCommandFlags flags, int startId)
20         {
21             messageNotification = new Notification();
22             messageNotification.Icon = Resource.Drawable.Icon;
23             messageNotification.TickerText = new Java.Lang.String("新消息");
24             messageNotification.Defaults = NotificationDefaults.Sound;
25             messageNotificatioManager = this.GetSystemService(Service.NotificationService) as NotificationManager;
26             messageIntent = new Intent(this, new Activity2().Class);
27             messagePendingIntent = PendingIntent.GetActivity(this, 0, messageIntent, 0);
28 
29             //开启线程
30             messageThread = new MessageThread();
31             messageThread.isRunning = true;
32             messageThread.Start();
33 
34             return base.OnStartCommand(intent, flags, startId);
35         }
36     }
37 
38     public class MessageThread : Thread
39     {
40         private Notification messageNotification = null;
41 
42         public bool isRunning = true;
43         public void run()
44         {
45             while (isRunning)
46             {
47                 try
48                 {
49                     Thread.Sleep(3600);
50                     string serverMessage = getServerMessage();
51                     if (string.IsNullOrEmpty(serverMessage))
52                     {
53                         //new Java.Lang.String("奥巴马宣布,本拉登兄弟挂了!" + serverMessage)
54                         messageNotification.SetLatestEventInfo(new MessageService(), new Java.Lang.String("新消息"), new Java.Lang.String("奥巴马宣布,本拉登兄弟挂了!" + serverMessage), PendingIntent.GetActivity(new MessageService(), 0, new Intent(new MessageService(), new Activity2().Class), 0));
55                     }
56                 }
57                 catch (System.Exception)
58                 {
59                     throw;
60                 }
61             }
62         }
63 
64         public string getServerMessage()
65         {
66             return "YES!";
67 
68         }
69     }

调用方式

 1 [Activity(Label = "MonoForAndroidFirst", MainLauncher = true, Icon = "@drawable/icon")]
 2     public class Activity1 : Activity, View.IOnClickListener
 3     {
 4         NotificationManager nm;
 5         protected override void OnCreate(Bundle bundle)
 6         {
 7             //调用父类的方法完成页面公共的功能
 8             base.OnCreate(bundle);
 9             //设置布局页面
10             SetContentView(Resource.Layout.Main);
11             bool isMessagePush = true;//不开启就设置为false;
12             if(isMessagePush){
13                 StartService(new Intent(this, new MessageService().Class));
14             };
15      }
chhnanglee的主页 chhnanglee | 初学一级 | 园豆:195
提问于:2013-09-09 11:19
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册