我是android新手。我想构建一个在第一个活动开始时启动服务的应用程序。当我离开主要活动时,该服务想留下来。因为下一个Activity使用该服务来做一些操作。
所以我为此写了一些代码。 (但我没有写第二个活动的过程)。 当我点击下一个按钮(转到下一个活动)第二个活动启动并获得无响应消息。
logcast说。
Unable to stop activity {com.example.autocomplete.app/com.example.autocomplete.app.MyActivity}: java.lang.IllegalArgumentException: Service not registered: com.example.autocomplete.app.MyActivity$1@534506f8
请帮助纠正此错误。 除了这种方式,我们有什么方法可以做上述情况。
所以我的代码如下所示
package com.example.autocomplete.app;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MyActivity extends Activity {
EditText txtA;
EditText txtB;
EditText txtResult;
Button btnAns;
MySumService mservice;
boolean mbound;
ServiceConnection mcontn=new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName componentName, IBinder service) {
mbound=true;
MySumService.LocalBinder binder=(MySumService.LocalBinder)service;
mservice=binder.getService();
}
@Override
public void onServiceDisconnected(ComponentName componentName) {
mbound=false;
mservice=null;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
txtA=(EditText)findViewById(R.id.txtA);
txtB=(EditText)findViewById(R.id.txtB);
txtResult=(EditText)findViewById(R.id.txtResult);
Button btnans=(Button)findViewById(R.id.button);
btnans.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int a=Integer.parseInt(txtA.getText().toString());
int b=Integer.parseInt(txtB.getText().toString());
int r=mservice.Sum(a,b);
txtResult.setText(new String().valueOf(r));
}
});
Button btnx=(Button)findViewById(R.id.button2);
btnx.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
Intent p=new Intent(MyActivity.this,next.class);
startActivity(p);
}
});
Intent i=new Intent(this,MySumService.class);
startService(i);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mbound){
mservice.unbindService(mcontn);
mbound=false;
}
}
@Override
protected void onStart() {
super.onStart();
Intent i=new Intent(this,MySumService.class);
bindService(i,mcontn,BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
if (mbound){
mservice.unbindService(mcontn);
mbound=false;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
MySumService.java
package com.example.autocomplete.app;
import android.app.Service;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;
/**
* Created by NRV on 9/6/2014.
*/
public class MySumService extends Service {
private IBinder mBinder=new LocalBinder();
int k=0;
@Override
public void onCreate() {
super.onCreate();
}
@Override
public IBinder onBind(Intent intent) {
return mBinder;
}
public int Sum(int a, int b){
return k;
}
public class LocalBinder extends Binder{
public MySumService getService(){
return MySumService.this;
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
for (int ip=0;ip<1000;ip++){
k=ip;
}
Toast.makeText(getApplicationContext(),"Online",Toast.LENGTH_SHORT).show();
return super.onStartCommand(intent, flags, startId);
}
}
activity_my.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MyActivity">
<TextView
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Text 1"
android:id="@+id/textView"
android:layout_below="@+id/textView2"
android:layout_alignParentLeft="true"
android:layout_marginTop="58dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Text2"
android:id="@+id/textView3"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtA"
android:layout_below="@+id/textView"
android:layout_alignParentLeft="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtB"
android:layout_below="@+id/textView3"
android:layout_alignParentLeft="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/txtResult"
android:layout_below="@+id/txtB"
android:layout_alignParentLeft="true"
android:layout_marginTop="42dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_toRightOf="@+id/textView3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="---->"
android:id="@+id/button2"
android:layout_below="@+id/txtResult"
android:layout_toRightOf="@+id/textView2" />
</RelativeLayout>
lout2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="@+id/textView" />
</LinearLayout>
next.java
package com.example.autocomplete.app;
import android.app.Activity;
import android.os.Bundle;
/**
* Created by NRV on 9/6/2014.
*/
public class next extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lout2);
}
}
答案 0 :(得分:0)
如果您希望自己的服务在明确停止之前保持运行,则需要START_STICKY
中的onStartCommand()
:
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
handleCommand(intent);
// We want this service to continue running until it is explicitly
// stopped, so return sticky.
return START_STICKY;
}
这样,它将持续跨越各种活动。
现在例外:
java.lang.IllegalArgumentException: Service not registered
表示您在unbindService()
来电期间无需提供服务。