0

我是android新手。我想创建一个应用程序,在第一个活动开始时启动服务。当我离开主要活动时,该服务想留下来。因为下一个活动使用该服务来执行一些操作。Android服务未注册

所以我写了一些代码。 (但我没有写第二个活动的过程)。 当我点击下一步按钮(去下一个活动)第二次活动启动并得到没有响应的消息。

Logcast说。

Unable to stop activity {com.example.autocomplete.app/com.example.autocomplete.app.MyActivity}: java.lang.IllegalArgumentException: Service not registered: [email protected] 

请有人帮助纠正此错误。 有没有什么办法可以做到这一点,除了这种方式。

所以我的代码显示在下面

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

包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

这对我来说似乎有点像一个奇怪的实现。仅在与您绑定的活动生命周期回调中匹配的服务才能解除绑定。例如onResume/onPause或onStart/onStop。我真的不会依赖onDestroy来做任何事情。它可能永远不会被调用。 – 323go 2014-09-06 04:44:34

+0

@ 323go OP需要返回'START_STICKY',如果我没有错的话:) – 2014-09-06 05:07:02

+0

仔细看看代码,看起来这个服务在绑定服务(它应该通过'startService ()')和intent服务。 “START_STICKY”确实与坚持不同的活动无关,它只是告诉Android如果由于资源限制而丢弃服务该怎么做。 – 323go 2014-09-06 05:12:04

回答

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()通话中维修。