如何从android studio中的另一个活动(在module1中)启动服务(module2)?

时间:2015-05-15 14:31:48

标签: android

我在android studio中有一个项目有2个模块:穿戴和移动。在磨损模块中,我有一个WearService,在移动模块中,我有一个HandActivity。我想从HandActivity启动WearService,只需按下按钮。我该怎么做?我试图使用该代码,但它没有工作。

package com.example.joe.activitytoactivity;

public class WearService extends WearableListenerService {
public WearService() {
}


@Override
public int onStartCommand(Intent intent, int flags, int startId){
    super.onStartCommand(intent, flags, startId);
    return START_STICKY;
}

}

package com.example.joe.activitytoactivity;

public class HandActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hand);
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_hand, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

public void sendMessage(View view) {
    Intent intent = new Intent(HandActivity.this,com.example.joe.activitytoactivity.WearService.class);
    startActivity(intent);
}

}

问题出在哪里?我的android工作室说部分错了:Intent(HandActivity.this,com.example.joe.activitytoactivity.WearService.class

编辑:在Intent中,IDE说:无法解析符号“WearService”

OBS:对不起,可能是英文。

1 个答案:

答案 0 :(得分:0)

而不是

Intent intent = new Intent(HandActivity.this,com.example.ewelton.activitytoactivity.WearService.class)

尝试

Intent intent = new Intent(this,WearService.class);

此外,任何新的活动都需要在AndroidManifest中声明:

<activity
    android:name = "com.example.joe.activitytoactivity.WearService"
    android:label = "optionalname"
</activity>