繁体   English   中英

从另一个类在StartAtBootService上调用对象方法

[英]Calling an Object Method on StartAtBootService from another class

我似乎无法理解为什么在另一个名为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");
    }
     ...
}

假设StartAtBootService实际上应该在引导时启动,那么这里的代码几乎没有正确之处。

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

从策略上讲,服务很少会启动活动,而永远不会在启动时启动。

有什么方法可以调用此活动并将其写入create中,以便在启动时在后台显示?

不,对不起

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM