首页 > 移动平台 > 详细

【android】在Service中新建TextView

时间:2018-08-16 19:26:15      阅读:280      评论:0      收藏:0      [点我收藏+]
在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);
                    }

`

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

【android】在Service中新建TextView

原文:http://blog.51cto.com/5052416/2160847

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!