-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainActivity.java
91 lines (83 loc) · 3.3 KB
/
MainActivity.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
package com.sinantalebi.gcmnetworkmanager;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GoogleApiAvailability;
public class MainActivity extends AppCompatActivity {
private static final int REQUEST_GOOGLE_PLAY_SERVICES = 1972;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null)
startTaskService();
initViews();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_GOOGLE_PLAY_SERVICES:
if (resultCode == Activity.RESULT_OK) {
Intent i = new Intent(this, TaskService.class);
startService(i); // OK, init GCM
}
break;
default:
super.onActivityResult(requestCode, resultCode, data);
}
}
private void initViews() {
Button oneoff = (Button) findViewById(R.id.schedule_oneoff);
oneoff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TaskService.scheduleOneOff(v.getContext());
}
});
Button repeat = (Button) findViewById(R.id.schedule_repeating);
repeat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TaskService.scheduleRepeat(v.getContext());
}
});
Button cancelOneoff = (Button) findViewById(R.id.cancel_oneoff);
cancelOneoff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TaskService.cancelOneOff(v.getContext());
}
});
Button cancelRepeat = (Button) findViewById(R.id.cancel_repeat);
cancelRepeat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TaskService.cancelRepeat(v.getContext());
}
});
Button cancelAll = (Button) findViewById(R.id.cancel_all);
cancelAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TaskService.cancelAll(v.getContext());
}
});
}
private void startTaskService() {
GoogleApiAvailability api = GoogleApiAvailability.getInstance();
int isAvailable = api.isGooglePlayServicesAvailable(this);
if (isAvailable == ConnectionResult.SUCCESS) {
onActivityResult(REQUEST_GOOGLE_PLAY_SERVICES, Activity.RESULT_OK, null);
} else if (api.isUserResolvableError(isAvailable) &&
api.showErrorDialogFragment(this, isAvailable, REQUEST_GOOGLE_PLAY_SERVICES)) {
// wait for onActivityResult call
} else {
Toast.makeText(this, api.getErrorString(isAvailable), Toast.LENGTH_LONG).show();
}
}
}