请帮忙!我在这里绝望!!!

我在开始服务时遇到了麻烦。我将其移动到Activity中的一个内部类中,但无法启动它!

我不知道我是否需要更改清单中的内容或以其他方式调用它。

我尝试使用“ Intent intent = new Intent(HW07.this,PrimeService.class);”来调用它。我收到以下错误:

05-24 13:15:22.662:W / ActivityManager(80):无法启动服务意图{cmp = jschuler.cs211d.hw07 / .HW07 $ PrimeService(有其他功能)}:找不到

在没有HW07的情况下调用它时,会出现相同的错误。

有任何想法吗?这是我的代码:

package jschuler.cs211d.hw07;

import android.app.Activity;
import android.os.Bundle;
import android.view.*;
import android.widget.*;
import android.os.Handler;
import android.content.Intent;
import android.os.Message;
import android.util.Log;
import android.app.Service;
import android.os.IBinder;

public class HW07 extends Activity implements View.OnKeyListener
{
    String howMany, upper, lower, values;
    TextView tv_howMany, tvUpper, tvLower, test;
    EditText howManyInput, upperInput, lowerInput;
    //Handler h = new Handler();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        tv_howMany = (TextView) findViewById(R.id.how_many);
        tv_howMany.setText("How many primes would you like to generate?");

        howManyInput = (EditText) findViewById(R.id.input1);
        howManyInput.setOnKeyListener(this);

        tvUpper = (TextView) findViewById(R.id.upper);
        tvUpper.setText("Upper limit:");

        upperInput = (EditText) findViewById(R.id.input2);
        upperInput.setOnKeyListener(this);

        tvLower = (TextView) findViewById(R.id.lower);
        tvLower.setText("Lower limit:");

        lowerInput = (EditText) findViewById(R.id.input3);
        lowerInput.setOnKeyListener(this);

        test = (TextView) findViewById(R.id.test);
    }

    Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            Log.d("handleMessage","message handled!");
            Toast.makeText(getApplicationContext(), "in handleMessage", Toast.LENGTH_LONG).show();
            String primes = msg.obj.toString();
            Log.d("handleMessage!","Here are the primes: " + primes);
            super.handleMessage(msg);

        }
    };

    public boolean onKey(View v, int keyCode, KeyEvent ke)
    {
        if( (ke.getAction() == KeyEvent.ACTION_DOWN) &&
            (keyCode == KeyEvent.KEYCODE_ENTER) )
        {
            values = howManyInput.getText().toString() + "," +
                upperInput.getText().toString() + "," +
                lowerInput.getText().toString();
            test.setText(values);

            //handler.sendMessage(handler.obtainMessage(null, values));

            Intent intent = new Intent(this, PrimeService.class);
            intent.putExtra("parameters",values);
            startService(intent);

            return true;
        }
        return false;
    }

    public class PrimeService extends Service
    {
        @Override
        public IBinder onBind(Intent args)
        {
            return null;
        }

        @Override
        public int onStartCommand(Intent i, int flags, int startId)
        {
            Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();

            String parameters = i.getExtras().getString("parameters");
            Log.d("onStartCommand","parameters: " + parameters);
            //Handler serviceHandler = new Handler();

            String result = generatePrimes("Primes");
            //serviceHandler.sendMessage(serviceHandler.obtainMessage(0, result));

            handler.sendMessage(handler.obtainMessage(0, result));

            /* Thread t = new Thread( new Runnable()
            {
                public void run()
                {
                    String result = generatePrimes("Primes");

                    handler.sendMessage(handler.obtainMessage(0, result));
                }

            });
            t.start();
     */
            return START_STICKY;
        }

    /*     Handler serviceHandler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                Message primeMsg = serviceHandler.obtainMessage();
                String primes = primeMsg.obj.toString();

                //serviceHandler.sendMessage(serviceHandler.obtainMessage(0, result));

                Log.d("handleMessage","primes: "+primes);

                super.handleMessage(msg);
            }
        }; */

        public String generatePrimes(String p)
        {
            return p;
        }

        @Override
        public void onDestroy()
        {
            super.onDestroy();
            Toast.makeText(this,"Service Destroyed",Toast.LENGTH_LONG).show();
        }
    }
}


这是我的Android清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="jschuler.cs211d.hw07"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
        <activity android:name="HW07"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".PrimeService">
            <intent-filter>
                <action android:name="jschuler.cs211d.hw07"/>
            </intent-filter>
        </service>
    </application>
</manifest>


我在PrimeService中更改了一些代码...现在我只是想从Thread(在同一类中)获取String。我之前可以使用它,但是这次不起作用。

这是我的新代码:

package jschuler.cs211d.hw07;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import java.net.*;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

public class PrimeService extends Service
{
    String str;

    @Override
    public IBinder onBind(Intent args)
    {
        return null;
    }

    @Override
    public int onStartCommand(Intent i, int flags, int startId)
    {
        Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();

        String parameters = i.getExtras().getString("parameters");
        Log.d("onStartCommand","parameters: " + parameters);
        //Handler serviceHandler = new Handler();

        String result = generatePrimes("Primes");
        Log.d("result","result: "+result);
        Message primeMessage = receiveHandler.obtainMessage(0, result);
        receiveHandler.sendMessage(primeMessage);

        /* Handler sendHandler = new Handler()
        {
            @Override
            public void handleMessage(Message msg)
            {
                Log.d("sendHandler","handleMessage called");
                String result = generatePrimes("Primes");
                Message primeMessage = receiveHandler.obtainMessage();
                primeMessage.obj = result;
                receiveHandler.sendMessage(primeMessage);
                super.handleMessage(msg);
            }
        }; */

        // IF I GET THE ABOVE CODE TO WORK, I WILL PUT IT IN THIS THREAD!!!

        /* Thread t = new Thread( new Runnable()
        {
            public void run()
            {
                receiveHandler.sendMessage(receiveHandler.obtainMessage(0, generatePrimes("prime numbers")));

                //String result = generatePrimes("Primes");
                //serviceHandler.sendMessage(serviceHandler.obtainMessage(0, result));
            }

        });
        t.start(); */

        return START_STICKY;
    }

    Handler receiveHandler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            //serviceHandler.sendMessage(serviceHandler.obtainMessage(0, generatePrimes("prime numbers")));

            Message primeMsg = Message.obtain();
            //Message primeMsg = receiveHandler.obtainMessage();
            String primes = (String)primeMsg.obj;

            Log.d("handleMessage","here are the primes: "+primes);

            super.handleMessage(msg);
        }
    };

    public String generatePrimes(String p)
    {
        return p;
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        Toast.makeText(this,"Service Destroyed",Toast.LENGTH_LONG).show();
    }
}

最佳答案

您的Service必须是静态类。然后,一旦出现在清单中,就需要这样显示

<service android:name="HW07$PrimeService"/>


你可以做这样的事(伪)

class HM07 extends Activity {

    public static Handler mHandler;
    // ...

    @Override public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        // ...

        mHandler = new Handler();
    }
}

class PrimeService extends Service {
    @Override public void onStartCommand() {
        Handler handler = HM07.mHandler;
        handler.sendMessage(/* Message */);
    }
}

关于android - 无法在内部类中启动服务-这是我的Android list 吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10744881/

10-09 07:16