新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> Android 高级开发
    [返回] 中文XML论坛 - 专业的XML技术讨论区Android开发论坛『 Android 高级开发 』 → 【代码】Android 添加Notification给地震监视器 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 5754 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: 【代码】Android 添加Notification给地震监视器 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     挥戈回日 帅哥哟,离线,有人找我吗?魔羯座1987-12-26
      
      
      等级:大一(猛啃高等数学)
      文章:37
      积分:138
      门派:GOOGLEBBS.NET
      注册:2013/7/14

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给挥戈回日发送一个短消息 把挥戈回日加入好友 查看挥戈回日的个人资料 搜索挥戈回日在『 Android 高级开发 』 的所有贴子 访问挥戈回日的主页 引用回复这个贴子 回复这个贴子 查看挥戈回日的博客楼主
    发贴心情 【代码】Android 添加Notification给地震监视器

    在接下来的例子中,EarthquakeService将为每个新的地震触发一个Notification。显示状态条图标的同时,在扩展的状态窗口中显示地震的级别和位置,选择它将会打开Earthquake Activity。1. 在EarthquakeService中,创建一个新的Notification实例变量来储存Notification对象,用于控制状态条图标和扩展的状态窗口中项目的细节。Java代码:
    Java代码
    privateNotificationnewEarthquakeNotification;
    publicstaticfinalintNOTIFICATION_ID=1;
    扩展onCreate方法来创建Notification对象。java代码:
    Java代码
    @Override
    publicvoidonCreate(){
    updateTimer=newTimer(“earthquakeUpdates”);
    inticon=R.drawable.icon;
    StringtickerText=“NewEarthquakeDetected”;
    longwhen=System.currentTimeMillis();
    newEarthquakeNotification=newNotification(icon,tickerText,when);
    }
    现在,扩展annouceNewQuake方法,在每个新的地震数据添加到ContentProvider之后触发Notification。在初始化Notification之前,使用setLastestEventInfo方法来更新扩展的信息。java代码:
    Java代码
    privatevoidannounceNewQuake(Quakequake){
    StringsvcName=Context.NOTIFICATION_SERVICE;
    NotificationManagernotificationManager;
    notificationManager=(NotificationManager)getSystemService(svcName);
    Contextcontext=getApplicationContext();
    StringexpandedText=quake.getDate().toString();
    StringexpandedTitle=“M:”+quake.getMagnitude()+““+quake.getDetails();
    IntentstartActivityIntent=newIntent(this,Earthquake.class);
    PendingIntentlaunchIntent=PendingIntent.getActivity(context,0,startActivityIntent,0);
    newEarthquakeNotification.setLatestEventInfo(context,expandedTitle,expandedText,launchIntent);
    newEarthquakeNotification.when=java.lang.System.currentTimeMillis();
    notificationManager.notify(NOTIFICATION_ID,newEarthquakeNotification);
    Intentintent=newIntent(NEW_EARTHQUAKE_FOUND);
    intent.putExtra(“date”,quake.getDate().getTime());
    intent.putExtra(“details”,quake.getDetails());
    intent.putExtra(“longitude”,quake.getLocation().getLongitude());
    intent.putExtra(“latitude”,quake.getLocation().getLatitude());
    intent.putExtra(“magnitude”,quake.getMagnitude());
    sendBroadcast(intent);
    }
    最后一步是在两个Activity类中清除Notification。当应用程序活跃时,通过移除状态图标来完成。4.1. 在Earthquake Activity中,修改onCreate方法,获取NotificationManager的一个引用。java代码: Java代码 NotificationManagernotificationManager; @Override publicvoidonCreate(Bundleicicle){ [...existingonCreate...] StringsvcName=Context.NOTIFICATION_SERVICE; notificationManager=(NotificationManager)getSystemService(svcName); }
    4.2. 修改EarthquakeReceiver的onReceive方法。当这个方法执行时,正好是Activity活跃的时候,你可以在这里安全的取消所有的地震Notification。java代码:
    Java代码
    @Override
    publicvoidonReceive(Contextcontext,Intentintent){
    loadQuakesFromProvider();
    notificationManager.cancel(EarthquakeService.NOTIFICATION_ID);
    }
    4.3. 接下来,扩展onResume方法来取消Notification。java代码:
    Java代码
    @Override
    publicvoidonResume(){
    notificationManager.cancel(EarthquakeService.NOTIFICATION_ID);
    IntentFilterfilter;
    filter=newIntentFilter(EarthquakeService.NEW_EARTHQUAKE_FOUND);
    receiver=newEarthquakeReceiver();
    registerReceiver(receiver,filter);
    super.onResume();
    }
    4.4. 在EarthquakeMap Activity中重复相同的过程。java代码:
    Java代码
    NotificationManagernotificationManager;
    @Override
    publicvoidonCreate(Bundleicicle){
    super.onCreate(icicle);
    setContentView(R.layout.earthquake_map);
    ContentResolvercr=getContentResolver();
    earthquakeCursor=cr.query(EarthquakeProvider.CONTENT_URI,null,null,null,null);
    MapViewearthquakeMap=(MapView)findViewById(R.id.map_view);
    earthquakeMap.getOverlays().add(newEarthquakeOverlay(earthquakeCursor));
    StringsvcName=Context.NOTIFICATION_SERVICE;
    notificationManager=(NotificationManager)getSystemService(svcName);
    }
    @Override
    publicvoidonResume(){
    notificationManager.cancel(EarthquakeService.NOTIFICATION_ID);
    earthquakeCursor.requery();
    IntentFilterfilter;
    filter=newIntentFilter(EarthquakeService.NEW_EARTHQUAKE_FOUND);
    receiver=newEarthquakeReceiver();
    registerReceiver(receiver,filter);
    super.onResume();
    }
    publicclassEarthquakeReceiverextendsBroadcastReceiver{
    @Override
    publicvoidonReceive(Contextcontext,Intentintent){
    notificationManager.cancel(EarthquakeService.NOTIFICATION_ID);
    earthquakeCursor.requery();
    MapViewearthquakeMap=(MapView)findViewById(R.id.map_view);
    earthquakeMap.invalidate();
    }
    }

       收藏   分享  
    顶(0)
      




    ----------------------------------------------
    欢迎加入清源的android学习交流群,群号是: 278744577,加群时请验证:w3,谢谢!

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2013/7/24 16:12:00
     
     GoogleAdSense魔羯座1987-12-26
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 Android 高级开发 』 的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/4/19 11:06:46

    本主题贴数1,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    62.500ms