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