0

私はアンドロイドが初めてです。最初のアクティビティの開始時にサービスを開始するアプリを構築したいと考えています。そして、そのサービスは、私がメインの活動を離れたときにとどまりたいと思っています. 次のアクティビティはそのサービスを使用して何らかの操作を行うためです。

だから私はそのためのコードを書きます。(ただし、2番目のアクティビティのプロセスは書きませんでした)。次のボタンをクリックすると (次のアクティビティに移動します)、2 番目のアクティビティが起動し、応答していないというメッセージが表示されます。

ログキャストは言う。

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

パッケージ 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>

次の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);
}
}
4

1 に答える 1