Saturday, March 12, 2011

Create background service.

The following code uses buttons to call the service. you can start the service any way you want.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ButtonStart = new Button(this);
    ButtonStop = new Button(this);

    ButtonStart.setOnClickListener(this);
    ButtonStart.setId(1000);
    ButtonStart.setText("Start Service");
    
    ButtonStop.setOnClickListener(this);
    ButtonStop.setId(2000);
    ButtonStop.setText("Stop Service");

    
    LinearLayout OverView = new LinearLayout(this);
 OverView.setOrientation( LinearLayout.VERTICAL );
 OverView.setScrollContainer(true); 
 
 
 OverView.addView( ButtonStart );
 OverView.addView( ButtonStop);

 setContentView(OverView);
  }

  public void onClick(View src) {
    switch (src.getId()) {
    case 1000:
      Log.d(TAG, "onClick: starting srvice");
      startService(new Intent(this, myService.class));
      break;
    case 2000:
      Log.d(TAG, "onClick: stopping srvice");
      stopService(new Intent(this, myService.class));
      break;
    }
  }


You might want to set a service running in the background. The following code setup a notification in the background and check some numbers every seconds. If the number match a criteria, notification will be updated.
public void onCreate() {
  // The following define the notification bar area layout
  //Intent intent = new Intent(this, MainActivity.class);
  notificationManager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
  notification = new Notification();
  notification.icon = R.drawable.icon;
  notification.defaults = Notification.DEFAULT_SOUND;
  notification.tickerText = "Bu.......";
  notification.flags |= Notification.FLAG_AUTO_CANCEL;

  Intent notifyIntent = new Intent(getBaseContext(), ServiceDemo.class);
  PendingIntent PendTntent = PendingIntent.getActivity(this, 0, notifyIntent, android.content.Intent.FLAG_ACTIVITY_NEW_TASK);
  notification.setLatestEventInfo(this,"Title","Check the latest update",PendTntent);


  //notification = new Notification(R.drawable.icon,"test",null,"title",null);
  //PendingIntent contentIntent = PendingIntent.getActivity(this, 0, getIntent(), 0);
  
  
  // The following is something will run every 1 s.
  
         new Thread(new Runnable(){  
             int myCounter = (int) (msec%4000);
             @Override 
             public void run() {  
                 while (bThreadRunning) {  
                     try {  
                         Thread.sleep(1000);  
                     } catch (InterruptedException e) {  
                     }  
                     myCounter = (int) (msec%4000);
                     Log.i(TAG, "myservice running " + (msec+=1000) + "ms.");  
                     Log.i(TAG, "Your number is  " + (msec%4000) + " **");  

               if (myCounter == 0 ) {
                //notification.icon = R.drawable.icon;
                //notification.tickerText = "Bu.......";
                //notification.when = System.currentTimeMillis(); //通知产生的时间,会在通知信息里显示
                //notification.defaults = Notification.DEFAULT_SOUND;
                notification.tickerText = String.valueOf( msec );
                        Log.i(TAG, "Playing sound");  
                  notificationManager.notify(0, notification);

              }
               
                 }  
             }  
         }).start();  
         
     }  

Service onCreate running in the background all the time, if you want the service do something once when it starts, you can use the following code.
public void onStart(Intent intent, int startid) {
  Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
  Log.d(TAG, "onStart");
 }

No comments:

Post a Comment