document.write('
Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. public class MyService extends Service {
  2.  
  3.     private final Handler handler = new Handler();
  4.    
  5.     private int numIntent;
  6.    
  7.     // It\'s the code we want our Handler to execute to send data
  8.     private Runnable sendData = new Runnable() {
  9.         // the specific method which will be executed by the handler
  10.         public void run() {
  11.             numIntent++;
  12.            
  13.             // sendIntent is the object that will be broadcast outside our app
  14.             Intent sendIntent = new Intent();
  15.  
  16.             // We add flags for example to work from background        
  17. sendIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION|Intent.FLAG_FROM_BACKGROUND|Intent.FLAG_INCLUDE_STOPPED_PACKAGES  );
  18.            
  19.             // SetAction uses a string which is an important name as it identifies the sender of the itent and that we will give to the receiver to know what to listen.
  20.             // By convention, it\'s suggested to use the current package name
  21.             sendIntent.setAction("com.test.sendintent.IntentToUnity");
  22.            
  23.             // Here we fill the Intent with our data, here just a string with an incremented number in it.
  24.             sendIntent.putExtra(Intent.EXTRA_TEXT, "Intent "+numIntent);
  25.             // And here it goes ! our message is send to any other app that want to listen to it.
  26.             sendBroadcast(sendIntent);
  27.            
  28.             // In our case we run this method each second with postDelayed
  29.             handler.removeCallbacks(this);
  30.             handler.postDelayed(this, 1000);
  31.         }
  32.     };
  33.    
  34.     // When service is started
  35.     @Override
  36.     public void onStart(Intent intent, int startid) {
  37.         numIntent = 0;
  38.         // We first start the Handler
  39.         handler.removeCallbacks(sendData);
  40.         handler.postDelayed(sendData, 1000);
  41.     }
  42.    
  43. }
');