【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);
                    }

`

最終的驗證結果是可以正常顯示的。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章