闹钟AlarmManager和通知NotificationManager

来源:互联网 发布:淘宝上买东西不花钱 编辑:程序博客网 时间:2024/06/15 02:21
import android.app.AlarmManager;import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.TimePickerDialog;import android.content.Context;import java.util.Calendar;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.support.v7.app.NotificationCompat;import android.view.View;import android.widget.RemoteViews;import android.widget.TimePicker;public class SwjActivity extends AppCompatActivity {    private AlarmManager alarmManager;    private PendingIntent pendingIntent;    public NotificationManager mNotificationManager;    /** NotificationCompat 构造器*/    NotificationCompat.Builder mBuilder;    /** Notification 的ID */    int notifyId = 101;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_swj);        //获取闹钟管理者        alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);    }    //(一)设置闹钟(一次)    public void setAlarm(View view) {        //获取当前系统的时间        Calendar calendar = Calendar.getInstance();        int hour = calendar.get(Calendar.HOUR_OF_DAY);        int minute = calendar.get(Calendar.MINUTE);        TimePickerDialog timePickerDialog = new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {            @Override            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {                Calendar calendar1 = Calendar.getInstance();                calendar1.set(Calendar.HOUR_OF_DAY,hourOfDay);                calendar1.set(Calendar.MINUTE,minute);                Intent intent=new Intent();                intent.setAction("com.lsj.administrator.myapplicatio.swj");                pendingIntent = PendingIntent.getBroadcast(SwjActivity.this,0x110,intent,0);                alarmManager.set(AlarmManager.RTC_WAKEUP,calendar1.getTimeInMillis(), pendingIntent);            }        },hour,minute,true);        timePickerDialog.show();    }1.发广播(我这种) //时间一到,发送广播(闹钟响了) //广播接受者中(跳转Activity) // 跳转Activity,在这个Activity中播放音乐(前面博文有介绍播放音乐,在这就没写)2. 直接跳Activities     3. 跳Service服务    //(二)设置闹钟(周期)    public void setAlarmCycle(View view){        //获取当前系统的时间        Calendar calendar=Calendar.getInstance();        int hour=calendar.get(Calendar.HOUR_OF_DAY);        int minute=calendar.get(Calendar.MINUTE);        //弹出时间对话框        TimePickerDialog timePickerDialog=new TimePickerDialog(this, new TimePickerDialog.OnTimeSetListener() {            @Override            public void onTimeSet(TimePicker timePicker, int i, int i1) {                Calendar c=Calendar.getInstance();                c.set(Calendar.HOUR_OF_DAY,i);                c.set(Calendar.MINUTE,i1);                Intent intent=new Intent();                intent.setAction("com.lsj.administrator.myapplicatio.swj");                //将来时态的跳转  ang eng ing ong                pendingIntent = PendingIntent.getBroadcast(SwjActivity.this,0x101,intent,0);                //设置周期闹钟                alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),5000, pendingIntent);                //时间一到,发送广播(闹钟响了)                //广播接受者中(跳转Activity)                // 跳转Activity,在这个Activity中播放音乐            }        },hour,minute,true);        timePickerDialog.show();    }//(三)取消周期    public void cancelCycle(View view){        alarmManager.cancel(pendingIntent);    }//(四)发送通知(系统)    public void sendNotification(View view){        //实例化通知管理器        NotificationManager notificationManager= (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);        //实例化通知        NotificationCompat.Builder builder=new NotificationCompat.Builder(this);        builder.setContentTitle("情人节,听说你又变帅了");        builder.setContentText("那是必须的了,不然怎么还单身");        builder.setDefaults(NotificationCompat.DEFAULT_ALL);        builder.setAutoCancel(true);        builder.setSmallIcon(android.R.drawable.ic_media_play);        builder.setContentIntent(PendingIntent.getActivity(this,0x102,new Intent(this,MediaActivity.class),0));        Notification notification=builder.build();        //发送通知        notificationManager.notify(0x101,notification);    }//(五)发送自定义通知(仿今日头条)    public void sendMyselfNotification(View view){        //先设定RemoteViews        RemoteViews view_custom = new RemoteViews(getPackageName(), R.layout.view_custom);        //设置对应IMAGEVIEW的ID的资源图片        view_custom.setImageViewResource(R.id.custom_icon, R.drawable.icon);        view_custom.setTextViewText(R.id.tv_custom_title, "今日头条");        view_custom.setTextViewText(R.id.tv_custom_content, "金州勇士官方宣布球队已经聘请了主帅布鲁斯-卢,随后宣布了最后的结果。");        mBuilder = new NotificationCompat.Builder(this);        mBuilder.setContent(view_custom)                .setContentIntent(getDefalutIntent(Notification.FLAG_AUTO_CANCEL))                .setWhen(System.currentTimeMillis())// 通知产生的时间,会在通知信息里显示                .setTicker("有新资讯")                .setAutoCancel(true)                .setPriority(Notification.PRIORITY_DEFAULT)// 设置该通知优先级                .setOngoing(false)//不是正在进行的   true为正在进行  效果和.flag一样                .setSmallIcon(R.drawable.icon);        Notification notify = mBuilder.build();        notify.contentView = view_custom;        mNotificationManager.notify(notifyId, notify);    }    public PendingIntent getDefalutIntent(int flags){        PendingIntent pendingIntent= PendingIntent.getActivity(this, 1, new Intent(), flags);        return pendingIntent;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159

这里写图片描述
二:(下面贴上今日头条通知栏xml )

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="vertical" >    <ImageView        android:id="@+id/custom_icon"        android:layout_width="50dip"        android:layout_height="50dip"        android:layout_alignParentLeft="true"        android:layout_margin="5dip"        android:padding="5dip"        android:src="@drawable/icon" />    <LinearLayout        android:layout_width="match_parent"        android:layout_height="match_parent"        android:layout_alignParentTop="true"        android:layout_toRightOf="@id/custom_icon"        android:orientation="vertical" >        <RelativeLayout            android:layout_width="match_parent"            android:layout_height="wrap_content" >            <TextView                android:id="@+id/tv_custom_title"                style="@style/NotificationTitle"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_alignParentLeft="true"                android:layout_centerVertical="true"                android:text="title"                android:textSize="15sp" />            <TextView                android:id="@+id/tv_custom_time"                style="@style/NotificationTitle"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_alignParentRight="true"                android:layout_centerVertical="true"                android:text="04:10"                android:textSize="12sp" />        </RelativeLayout>        <TextView            android:id="@+id/tv_custom_content"            style="@style/NotificationContent"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:layout_marginTop="1dip"            android:text="content"            android:textSize="12sp" />    </LinearLayout></RelativeLayout>
原创粉丝点击