【android】在Service中新建TextView

在Activity中new TextView的时候,发现传入的参数是Context,不是必须为Activity,就想:在Service中新建一个View的话能否正常使用?

Service中:
`

public class MyJobService extends JobService {
        public static TextView myView;
        @Override
        public boolean onStartJob(JobParameters params) {
                myView = new TextView(this);
                myView.setText("在Service中新建");
                myView.setTextColor(Color.parseColor("#000000"));
                return false;
        }
        @Override
        public boolean onStopJob(JobParameters params) {
                return false;
        }

}
`

Activity的onCreate中:
`

    myView = MyJobService.myView;
                    if (myView != null) {
                            ((RelativeLayout)findViewById(R.id.activity_main)).addView(myView);
                    }

`

最终的验证结果是可以正常显示的。

猜你喜欢

转载自blog.51cto.com/5052416/2160847