-
Notifications
You must be signed in to change notification settings - Fork 0
/
SmsListenerService.java
62 lines (50 loc) · 1.75 KB
/
SmsListenerService.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
package com.example.demosmslistener;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.IBinder;
import android.telephony.PhoneStateListener;
import android.util.Log;
import android.widget.Toast;
public class SmsListenerService extends Service {
private static final int MY_PERMISSIONS_REQUEST_READ_PHONE_STATE = 1001;
private SmsReceiver smsReceiver;
@Override
public void onCreate() {
super.onCreate();
Toast.makeText(getApplicationContext(), "SMS Listerner Service", Toast.LENGTH_SHORT).show();
smsReceiver = new SmsReceiver();
IntentFilter intentFilter = new IntentFilter("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(smsReceiver, intentFilter);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Your background task code goes here
Log.d("MyBackgroundService", "Background task running");
new Thread(
new Runnable() {
@Override
public void run() {
while (true) {
Log.e("Service", "Service is running...");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
).start();
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
unregisterReceiver(smsReceiver);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}