github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/app/MyService.java (about) 1 package org.golang.app; 2 3 import android.app.Notification; 4 import android.app.PendingIntent; 5 import android.app.Service ; 6 import android.content.Intent; 7 import android.os.IBinder; 8 import android.util.Log; 9 import java.net.Socket; 10 import android.os.SystemClock; 11 12 13 public class MyService extends Service { 14 15 @Override 16 public IBinder onBind(Intent intent) { 17 Log.d("Go", "MyService onBind"); 18 return null; 19 } 20 21 public int onStartCommand(Intent intent, int flags, int startId) { 22 Log.d("Go", "MyService onStartCommand"); 23 SystemClock.sleep(500); 24 Intent i = new Intent(this, WViewActivity.class); 25 i.addFlags(i.FLAG_ACTIVITY_NEW_TASK); 26 startActivity(i); 27 28 return super.onStartCommand(intent, flags, startId); 29 } 30 31 public void onStart() { 32 Log.d("Go", "MyService onStart"); 33 } 34 35 public void onDestroy() { 36 Log.d("Go", "MyService onDestroy"); 37 super.onDestroy(); 38 } 39 40 @Override 41 public void onCreate() { 42 Log.d("Go", "MyService onCreate"); 43 super.onCreate(); 44 45 ShortcutIcon(); 46 47 sendNotif(); 48 49 50 //Runnable r = new Runnable() { 51 // public void run() { 52 GoNativeActivity.load(); 53 // } 54 //}; 55 //Thread t = new Thread(r); 56 //t.start(); 57 58 Intent dialogIntent = new Intent(this, GoNativeActivity.class); 59 dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 60 startActivity(dialogIntent); 61 62 63 /* 64 try { 65 Intent intent1 = new Intent(Intent.ACTION_VIEW); 66 Uri data = Uri.parse("http://localhost:8089"); 67 intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 68 intent1.setData(data); 69 startActivity(intent1); 70 } catch (Exception e) { 71 Log.e("Go", "http://localhost:8089 failed", e); 72 }*/ 73 } 74 75 76 void sendNotif() { 77 78 Log.d("Go", "MyService sendNotif"); 79 Notification notif = new Notification(R.drawable.icon, "Dcoin", System.currentTimeMillis()); 80 81 Intent intent = new Intent(this, MainActivity.class); 82 PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent, 0); 83 84 notif.setLatestEventInfo(this, "Dcoin", "Running", pIntent); 85 86 startForeground(1, notif); 87 88 } 89 private void ShortcutIcon(){ 90 91 Log.d("Go", "MyService ShortcutIcon"); 92 Intent shortcutIntent = new Intent(getApplicationContext(), MainActivity.class); 93 shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); 94 shortcutIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 95 96 Intent addIntent = new Intent(); 97 addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent); 98 addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Dcoin"); 99 addIntent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, Intent.ShortcutIconResource.fromContext(getApplicationContext(), R.drawable.icon)); 100 addIntent.setAction("com.android.launcher.action.INSTALL_SHORTCUT"); 101 getApplicationContext().sendBroadcast(addIntent); 102 } 103 104 public static boolean DcoinStarted(int port) { 105 for (int i=0;i<60;i++) { 106 try (Socket ignored = new Socket("localhost", port)) { 107 return true; 108 } catch (Exception ignored) { 109 SystemClock.sleep(500); 110 } 111 } 112 return true; 113 } 114 115 }