I am trying to display a window from service but don't getting it how to do this
Here is my code--
In onStart
method of Service
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.main1, null);
WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.type = 2002;
params.format = 1;
params.flags = 40;
params.height = WindowManager.LayoutParams.FILL_PARENT;
params.width = WindowManager.LayoutParams.FILL_PARENT;
manager.addView(view, params);
/** just a timer *****/
textView = (TextView) view.findViewById(R.id.textView1);
final Timer t = new Timer("hello", true);
t.schedule(new TimerTask() {
@Override
public void run() {
textView.post(new Runnable() {
public void run() {
seconds++;
if (seconds == 60) {
seconds = 0;
minute++;
}
if (minute == 60) {
minute = 0;
hour++;
}
textView.setText(""
+ (hour > 9 ? hour : ("0" + hour)) + " : "
+ (minute > 9 ? minute : ("0" + minute))
+ " : "
+ (seconds > 9 ? seconds : "0" + seconds));
}
});
}
}, 1000, 1000);
view.findViewById(R.id.stop).setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
t.cancel();
}
});
But nothing shown up. How can i display my views in window from service?
Wooohoooo Got it work The trick was just using system window service
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FILL_PARENT, 150,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER;
add the permission
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Key handling can also be implemented in service !! Generally if WindowManager is used to display view in service it does'nt take any key events most of the time !!
But you can make an interface with 'dispatchKeyEvent' or any other key handling methods(onKeyUp, onKeyDown etc.) and implement them is service.
Happy coding.
来源:https://stackoverflow.com/questions/12650463/getting-window-display-from-service-android