-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAlarmService.java
185 lines (131 loc) · 5.29 KB
/
AlarmService.java
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package com.example.user.standartaalarmclock;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.widget.Toast;
public class AlarmService extends Service {
MediaPlayer mediaPlayer;
private int startId;
private boolean isRunning;
private Context context;
private static final int NOTID=1;
public AlarmService() {
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@RequiresApi(api = Build.VERSION_CODES.O)
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(this, "service starting", Toast.LENGTH_SHORT).show();
String state=intent.getExtras().getString("extra");
assert state != null;
switch (state) {
case "yes":
startId = 1;
break;
case "No":
startId = 0;
break;
default:
startId = 0;
break;
}
if (!this.isRunning && startId==1)
{
Log.e("there is no music","and you want start");
mediaPlayer=MediaPlayer.create(this,R.raw.sleepy);
mediaPlayer.setLooping(true);
mediaPlayer.start();
sendNotification(" woke! up! woke! up! ");
Intent i = new Intent();
i.setClass(this, Pop.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
Toast.makeText(this, "there is no music and you want start", Toast.LENGTH_SHORT).show();
this.isRunning=true;
this.startId=0;
}
else if(this.isRunning && startId==0)
{
Log.e("there is music","and you want stop");
mediaPlayer.stop();
mediaPlayer.reset();
sendNotification("");
Toast.makeText(this, "there is music and you want stop ", Toast.LENGTH_SHORT).show();
this.isRunning=false;
this.startId=0;
}
else if (!this.isRunning && startId ==0)
{
Log.e("there is no music","and you want stop");
Toast.makeText(this, "there is no music and you want stop ", Toast.LENGTH_SHORT).show();
this.isRunning=false;
this.startId=0;
stopSelf();
}
else if(this.isRunning && startId==1)
{
Log.e("there is music","and you want start");
Toast.makeText(this, "there is music and you want start", Toast.LENGTH_SHORT).show();
this.isRunning=true;
this.startId=1;
}
else {
Log.e("else "," somhow any problem ");
}
return START_NOT_STICKY;
}
@Override
public void onDestroy() {
Log.e("OnDestroy process "," somhow any problem "+this.isRunning);
super.onDestroy();
this.isRunning=false;
}
private void sendNotification(String msg) {
int NOTIFICATION_ID = 234;
String CHANNEL_ID = "my_channel_01";
Log.d("AlarmService "," Alarm service preparing for send notificatio "+ msg);
NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
CharSequence name = "my_channel";
String Description = "This is my channel";
int importance = NotificationManager.IMPORTANCE_MIN;
NotificationChannel mChannel = new NotificationChannel(CHANNEL_ID, name, importance);
mChannel.setDescription(Description);
mChannel.enableLights(true);
mChannel.setLightColor(Color.RED);
mChannel.enableVibration(true);
mChannel.setVibrationPattern(new long[]{100, 200, 300, 400, 500, 400, 300, 200, 400});
mChannel.setShowBadge(false);
notificationManager.createNotificationChannel(mChannel);
}
NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(R.mipmap.ic_launcher)
.setContentTitle(" AALLARRMM NOTIFY ")
.setContentText(msg);
PendingIntent pendingIntent=PendingIntent.getActivity(this,0,new Intent(this,MainActivity.class),0);
builder.setContentIntent(pendingIntent);
//notificationManager.notify(NOTIFICATION_ID, builder.build());
/// startForeground(NOTIFICATION_ID,builder.build());
if (msg.equals("")){
stopSelf();
}
else {
startForeground(NOTIFICATION_ID,builder.build());
}
}
}