• android本地定时通知


    android本地通知略有不同,分为立即触发和延时触发

    1.即时通知

    android默认的Notification为立即触发

            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
                intent.setClass(_gameActivity, _gameActivity.getClass());
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
                PendingIntent content = PendingIntent.getActivity(context, 0, intent, 0);
                final NotificationManager notiMgr = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
                final Notification noti = new Notification(R.drawable.icon, notiResult.getPayload(), System.currentTimeMillis());
                String app_name = _gameActivity.getResources().getString(R.string.app_name);
                noti.setLatestEventInfo(context, app_name, notiResult.getPayload(), content);
    
                notiMgr.notify(atomicInteger.incrementAndGet(), noti);

    2.延时触发

    想让一定时间后再触发通知,其实是用系统的AlarmManager来实现的。

    具体的AlarmManager解释参考

    http://yuanzhifei89.iteye.com/blog/1131523

    http://blog.csdn.net/ryantang03/article/details/9317499

    添加一个ALARM_SERVICE

    /*
         * name:通知名字,作为通知id使用
         * content:通知内容
         * time:倒时时(秒)
         * */
        public static void addLocalNotication(String name, String content, int time)
        {
            Calendar cal = Calendar.getInstance();
            cal.setTimeInMillis(System.currentTimeMillis());  
            cal.add(Calendar.SECOND, (int) time);  
    
            Activity activity = _gameActivity;
            Intent intent = new Intent(activity, NotificationReceiver.class);
            intent.setClass(activity, NotificationReceiver.class);  
            intent.setData(Uri.parse(name));

          intent.putExtra("msg", "play_hskay");

             intent.putExtra("content", content);

         
            PendingIntent pi = PendingIntent.getBroadcast(activity, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
                 
            AlarmManager am = (AlarmManager) activity.getSystemService(Context.ALARM_SERVICE);
            am.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pi);
        }

    添加一个系统通知类,用于在倒计时间到时,接收系统轮询通知

    import java.util.List;
    import java.util.concurrent.atomic.AtomicInteger;
    
    import android.app.ActivityManager;
    import android.app.ActivityManager.RunningAppProcessInfo;
    import android.app.ActivityManager.RunningTaskInfo;
    import android.app.Notification;
    import android.app.NotificationManager;
    import android.app.PendingIntent;
    import android.content.BroadcastReceiver;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.content.IntentFilter;
    import android.content.pm.PackageInfo;
    import android.content.pm.ResolveInfo;
    import android.content.pm.PackageManager.NameNotFoundException;
    import android.util.Log;
    import android.widget.Toast;
    
    public class NotificationReceiver extends BroadcastReceiver
    {
        public NotificationReceiver(){}
        
        
    
        @Override
        public void onReceive(Context context, Intent itent) {
            //Toast.makeText(context, "闹铃响了, 可以做点事情了~~", Toast.LENGTH_LONG).show();  
            String msg = itent.getStringExtra("msg");
            String content = itent.getStringExtra("content");
          //判断应用是否在前台运行 
            if (isTopActivity(context)) {
                return;
            }
           //推送一条通知
            shownotification(context,content);
            return;
        }
        
        
        public void shownotification(Context context, String msg)  
        {  
            NotificationManager barmanager=(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);  
            Notification notice = new Notification(R.drawable.icon,msg,System.currentTimeMillis());  
            notice.flags=Notification.FLAG_AUTO_CANCEL;  
            
            Intent appIntent = new Intent(Intent.ACTION_MAIN);  
            appIntent.addCategory(Intent.CATEGORY_LAUNCHER);  
            appIntent.setComponent(new ComponentName(context.getPackageName(), context.getPackageName() + "." + "Splash"));   
            //设置启动模式  
            appIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK| Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0,
                    appIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            String app_name = context.getResources().getString(R.string.app_name);
            notice.setLatestEventInfo(context, app_name, msg, contentIntent);
            AtomicInteger atomicInteger = new AtomicInteger(1);
            barmanager.notify(0, notice);
        } 
        
        protected static boolean isTopActivity(Context activity){
            String packageName = activity.getPackageName();
            ActivityManager activityManager = (ActivityManager) activity.getSystemService(Context.ACTIVITY_SERVICE);
            List<RunningTaskInfo>  tasksInfo = activityManager.getRunningTasks(1);  
            if(tasksInfo.size() > 0){  
                Log.d("test","---------------包名-----------"+tasksInfo.get(0).topActivity.getPackageName());
                //应用程序位于堆栈的顶层  
                if(packageName.equals(tasksInfo.get(0).topActivity.getPackageName())){  
                    return true;  
                }  
            }  
            return false;
        }
    
    }
    <receiver android:name=".NotificationReceiver" 
                android:permission="com.google.android.c2dm.permission.SEND">
                <intent-filter>
                    <action android:name="play_hskay" />
                </intent-filter>
            </receiver>
    淡泊明志,宁静致远
  • 相关阅读:
    c#pc上测试微信端企业公众商城个人中心链接的工具JMeter
    eclipse java tomcat 远程调试
    Java多线程-线程池ThreadPoolExecutor构造方法和规则
    servlet3异步原理与实践
    eclipse Filter web.xml 问题解决 异步请求@WebServlet
    c# maiform父窗体改变动态的gridew 奇偶行变色的快捷方法
    java redispool测试类保存
    c#dev gridview 设置隔行换色等
    c#devexpress 窗体控件dock的重要
    devexpress总结 accordionControl 加载panelcontrol 的快捷方式
  • 原文地址:https://www.cnblogs.com/lycokcc/p/4112214.html
Copyright © 2020-2023  润新知