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.发广播(我这种) 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"); pendingIntent = PendingIntent.getBroadcast(SwjActivity.this,0x101,intent,0); alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),5000, pendingIntent); } },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 view_custom = new RemoteViews(getPackageName(), R.layout.view_custom); 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) .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>