从另一个类调用StartAtBootService上的Object方法

时间:2011-09-06 18:07:15

标签: java android service

我似乎无法理解为什么在另一个名为StartAtBootService的类中调用ApolloMobileActivity中的方法并不起作用:

我创建了一个名为ApolloMobileActivity的类

public class ApolloMobileActivity extends Activity {
int j = 0;  

    public void callLog(){

        TextView tv = new TextView(this);
        tv.setMovementMethod(new ScrollingMovementMethod());
        setContentView(tv);

            tv.setText("Some Text");
     }
  }

现在在我的StartAtBootService类中,我想调用' callLog()'方法所以,

 public class StartAtBootService extends Service
 {
private static final String TAG = "StartAtBootService";
ApolloMobileActivity callObject = new ApolloMobileActivity();

    //**This is where I am getting the error: Syntax error on token "callLog", Identifier expected after this token**
    callObject.callLog();

    //I am thinking that I can call this 'callObject' in onCreate to allow my service 
    //to run through my activity. I know I can use something like:
    //  Intent start = new Intent(context, ApolloMobileActivity.class);  
    //  start.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    //  context.startActivity(start);
    //But this will just start the Activity at boot in the foreground.
    //Is there some way to call this write this activity in on create so that it will show
    //in the background at boot-up?

    public IBinder onBind(Intent intent)
    {
        return null;
    }

    @Override
    public void onCreate() 
    {
                    //Some code here...
        Log.d(TAG, "onCreate");
    }
     ...
}

1 个答案:

答案 0 :(得分:0)

假设StartAtBootService确实应该在启动时启动,那么这里的代码几乎没有任何正确的答案。

从战术上讲,您无法通过其构造函数创建活动,也无法在活动中调用setContentView()之前调用onCreate()。您的服务在语法上也是无效的,因为您在方法外部有一个Java语句(callObject.callLog();)。

从战略上讲,服务很少应该启动活动,而且从不在启动时。

  

是否有某种方法可以在创建时调用此活动,以便在启动时在后台显示?

不,抱歉。