0

I want to make a view that updates its contents and shows regularly on the top of the windows. I found an example that makes a view on the top of the windows (http://blog.daum.net/mailss/18). The activity makes a UI and if I touched the start service button, the view shows its text. But when the view has created, it doesn't update its contents. I tried to add simple code that counts the number of view updating, but it doesn't work. How can I change the contents of the view regularly? Should I add a loop code in the service? I need your advice, thank you.

1.AlwaysOnTopActivity.java

public class AlwaysOnTopActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    findViewById(R.id.start).setOnClickListener(this);  //start button  
    findViewById(R.id.end).setOnClickListener(this);    //stop button       
}

@Override
public void onClick(View v) {
    int view = v.getId();
    if(view == R.id.start)
        startService(new Intent(this, AlwaysOnTopService.class));   
    else
        stopService(new Intent(this, AlwaysOnTopService.class));    
}

}

2.AlwaysOnTopService.java

public class AlwaysOnTopService extends Service {
private TextView tv;                                            
@Override
public IBinder onBind(Intent arg0) { return null; }

@Override
public void onCreate() {
    super.onCreate();

    tv = new TextView(this);        //creating view
    tv.setText("This view is always on top.");
    tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
    tv.setTextColor(Color.BLUE);


    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY, //to always on top
        WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,//for touch event
        PixelFormat.TRANSLUCENT);                                                   

    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);    
    wm.addView(tv, params);//We must configure the permission in the manifest
}

@Override
public void onDestroy() {
    super.onDestroy();
    if(tv != null)      //terminating the view
    {
        ((WindowManager) getSystemService(WINDOW_SERVICE)).removeView(tv);
        tv = null;
    }
}

}

1 Answers1

1

Use LocalBroadcastManager as described here. Update your UI in onReceive method

Community
  • 1
  • 1
Veaceslav Gaidarji
  • 4,261
  • 3
  • 38
  • 61