android-通知Notification

发送通知android

public class MyActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        
        String serviceContextName = Context.NOTIFICATION_SERVICE;
        NotificationManager notificationManager;
        notificationManager = (NotificationManager) getSystemService(serviceContextName);
        
        //设置图标,标题,时间
        int icon = R.drawable.ic_launcher;
        String tickerText = "Notification";
        long when = System.currentTimeMillis();
        
        Notification notification = new Notification(icon, tickerText, when);
        notification.number++;
    
        //设置展开通知的内容
        Context context = getApplicationContext();
        String expandedText = "Extended status text";
        String expandedTitle = "Notification Title";
        
        //当单击展开的文本时,用于启动一个活动的意图
        Intent intent = new Intent(this, MyActivity.class);
        PendingIntent launchIntent = PendingIntent.getActivity(context, 0, intent, 0);
        notification.setLatestEventInfo(context, expandedText, expandedText, launchIntent);
        
        //若要定制布局(若手动设置contentView时,必须同时设置contentIntent)
        notification.contentView = new RemoteViews(this.getPackageName(), R.layout.my_status_window_layout);
        notification.contentIntent = launchIntent;
        
        //修改通知中的视图
        notification.contentView.setImageViewResource(R.id.status_icon, R.drawable.ic_launcher);//将后者搬到前者这个id的视图上
        notification.contentView.setTextViewText(R.id.status_text, "Current Progress");
        notification.contentView.setProgressBar(R.id.status_progress, 100, 50, false);
        
        //触发通知
        int notificationRef = 1;
        notificationManager.notify(notificationRef, notification);
        
        //消除通知
        notificationManager.cancel(notificationRef);
        
        notification.defaults = Notification.DEFAULT_SOUND | Notification.DEFAULT_VIBRATE; //设置上默认的声音和震动
        
        //自定义通知声音
        Uri ringURI = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        notification.sound = ringURI;
        
        //自定义通知震动,需添加一个permission <uses-permission android:name="android.permission.VIBRATE" />
        long[] vibrate = new long[] {1000,1000,1000,1000,1000};//一个值震动多久,下一个值暂定多久,这里持续了5秒
        notification.vibrate = vibrate;
        
        //自定义通知闪屏
        notification.ledARGB = Color.RED;
        notification.ledOffMS = 0;//设置频率
        notification.ledOnMS = 1; //若OnMS也是0,则将灯关闭
        notification.flags = notification.flags | Notification.FLAG_SHOW_LIGHTS;
        
        //持续的通知
        notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;//正在进行的事件
        //连续的通知
        notification.flags = notification.flags | Notification.FLAG_INSISTENT;//一直重复音频震动闪烁直到被取消
    }
}

警报ide

 

public class MyActivity extends Activity {
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        
        //警报。警报类型:
//        RTC_WAKEUP指定时间唤醒并触发意图;
//        RTC指定时间触发意图但不唤醒设备;
//        ELAPSED_REALTIME设备启动后通过的时间触发待处理的意图,但不唤醒设备;
//        ELAPSED_REALTIME_WAKEUP设备启动并通过指定时间后唤醒设备并触发意图
        AlarmManager alarms = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
        int alarmType = AlarmManager.ELAPSED_REALTIME_WAKEUP;
        long timeOrLengthofWait = 10000;
        String ALARM_ACTION = "ALARM_ACTION";
        Intent intentToFire = new Intent(ALARM_ACTION);
        PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, intentToFire, 0);
        alarms.set(alarmType, timeOrLengthofWait, pendingIntent);
        
        //取消报警
        alarms.cancel(pendingIntent);
        
        //重复报警
        //若是已经唤醒,那么每小时触发一次意图
        alarms.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 60*60*1000, 60*60*1000, pendingIntent);
        //每小时唤醒并触发一个警报
        alarms.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, 60*60*1000, AlarmManager.INTERVAL_DAY, pendingIntent);
    }
}