在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);
}
`
最終的驗證結果是可以正常顯示的。