forked from democratic-coin/dcoin-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MyService.java
159 lines (128 loc) · 4.95 KB
/
MyService.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
package org.golang.app;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.Service ;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import java.lang.System;
import java.net.Socket;
import android.os.SystemClock;
import java.net.HttpURLConnection;
import java.net.URL;
import java.io.OutputStream;
public class MyService extends Service {
@Override
public IBinder onBind(Intent intent) {
Log.d("JavaGo", "MyService onBind");
return null;
}
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("JavaGo", "MyService onStartCommand");
Runnable r = new Runnable() {
public void run() {
SystemClock.sleep(500);
Intent i = new Intent(MyService.this, XWalkActivity.class);
i.addFlags(i.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
};
Thread t = new Thread(r);
t.start();
return super.onStartCommand(intent, flags, startId);
}
public void onStart() {
Log.d("JavaGo", "MyService onStart");
}
public void onDestroy() {
Log.d("JavaGo", "MyService onDestroy");
super.onDestroy();
}
@Override
public void onCreate() {
Log.d("JavaGo", "MyService onCreate");
super.onCreate();
ShortcutIcon();
sendNotif();
//Runnable r = new Runnable() {
// public void run() {
GoNativeActivity.load();
// }
//};
//Thread t = new Thread(r);
//t.start();
Intent dialogIntent = new Intent(this, GoNativeActivity.class);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
/*
try {
Intent intent1 = new Intent(Intent.ACTION_VIEW);
Uri data = Uri.parse("http://localhost:8089");
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent1.setData(data);
startActivity(intent1);
} catch (Exception e) {
Log.e("JavaGo", "http://localhost:8089 failed", e);
}*/
}
@SuppressWarnings("deprecation")
void sendNotif() {
Log.d("JavaGo", "MyService sendNotif");
// Notification notif = new Notification(R.drawable.icon, "Dcoin", System.currentTimeMillis());
Notification.Builder builder = new Notification.Builder(this);
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0);
builder.setAutoCancel(false);
builder.setContentTitle("Dcoin");
builder.setSmallIcon(R.drawable.icon);
builder.setContentIntent(pIntent);
builder.setOngoing(true);
builder.setContentText(Long.toString(System.currentTimeMillis()));
// builder.setNumber(System.currentTimeMillis());
builder.build();
Notification notif = builder.getNotification();
// notif.setLatestEventInfo(this, "Dcoin", "Running", pIntent);
startForeground(1, notif);
}
private void ShortcutIcon(){
Log.d("JavaGo", "MyService ShortcutIcon");
Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent addIntent = new Intent();
addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Dcoin");
addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon));
addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
getApplicationContext().sendBroadcast(addIntent);
}
public static boolean DcoinStarted(int port) {
for (int i=0;i<35;i++) {
try {
URL url = new URL("http://localhost:8089/");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
Log.d("JavaGo", "0");
connection.setRequestMethod("GET");
Log.d("JavaGo", "1");
connection.connect();
Log.d("JavaGo", "2");
Log.d("JavaGo", "huc.getResponseCode()"+connection.getResponseCode());
if (connection.getResponseCode() == 200 ) {
return true;
} else {
SystemClock.sleep(500);
}
} catch (Exception e) {
Log.e("JavaGo", "http://localhost:8089 failed", e);
SystemClock.sleep(500);
}
/*
try (Socket ignored = new Socket("localhost", port)) {
return true;
} catch (Exception ignored) {
SystemClock.sleep(500);
}*/
}
return true;
}
}