Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/178.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法在内部类中启动服务-它是我的Android清单吗?_Android_Service - Fatal编程技术网

无法在内部类中启动服务-它是我的Android清单吗?

无法在内部类中启动服务-它是我的Android清单吗?,android,service,Android,Service,请帮忙!我在这里绝望了 我无法开始我的服务。我把它移到了我活动中的一个内部类,但我无法启动它 我不知道我是否必须改变我清单中的某些东西,或者用不同的方式来称呼它 我尝试使用“Intent Intent=new Intent(HW07.this,PrimeService.class);”调用它,但出现以下错误: 05-24 13:15:22.662:W/ActivityManager(80):无法启动服务意图{cmp=jschuler.cs211d.hw07/.hw07$PrimeService(

请帮忙!我在这里绝望了

我无法开始我的服务。我把它移到了我活动中的一个内部类,但我无法启动它

我不知道我是否必须改变我清单中的某些东西,或者用不同的方式来称呼它

我尝试使用“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>

您的
服务
必须是静态类。然后,一旦在你的清单中,你将需要如此显示它

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

您的
服务
必须是静态类。然后,一旦在你的清单中,你将需要如此显示它

<service android:name="HW07$PrimeService"/>
您需要在下面向Androidmanifest.xml声明

<service android:name=".outclass$innerService"/>

注意美元符号

您需要在下面向Androidmanifest.xml声明

<service android:name=".outclass$innerService"/>


注意美元符号

啊,一个静态类!这是有道理的,但是。。。当我将其声明为静态类时,我得到一个错误:无法从静态上下文[javac]handler.sendMessage(handler.obtainMessage(0,result))引用非静态变量handler;[javac]^也许内部类不是解决问题的方法。我无法让活动和服务以其他方式进行通信,因为处理程序与不同的线程(因此与不同的消息队列)关联,我无法让它们进行通信。你知道我能做什么吗?没错。一般来说,拥有一个静态的内部服务不是一个好主意,因为通常情况下,它没有任何意义。服务应该是通过ServiceConnection进行通信的自给自足的实体。使用服务连接,您将收到一个服务实例。使用这个实例是你可以来回“交谈”的方式。不幸的是,这是一个类,我们不能使用ServiceConnection,因为我们还没有了解它!有没有办法只用一个处理程序和线程就可以做到这一点呢?我基本上是在PrimeService.java的内部类中提供代码。我无法让处理程序通信,因为它们有两个单独的消息队列!我知道这一定是可行的,但我能想到的唯一方法是使用意图发送消息。但这似乎毫无意义。。。还有,谢谢你的帮助!好吧,也许我可以使用ServiceConnection。与其坐在这里试图弄明白这一点,不如使用一些有效的方法,并且可能会因此失去一些分数。谢谢啊,一个静态类!这是有道理的,但是。。。当我将其声明为静态类时,我得到一个错误:无法从静态上下文[javac]handler.sendMessage(handler.obtainMessage(0,result))引用非静态变量handler;[javac]^也许内部类不是解决问题的方法。我无法让活动和服务以其他方式进行通信,因为处理程序与不同的线程(因此与不同的消息队列)关联,我无法让它们进行通信。你知道我能做什么吗?没错。一般来说,拥有一个静态的内部服务不是一个好主意,因为通常情况下,它没有任何意义。服务应该是通过ServiceConnection进行通信的自给自足的实体。使用服务连接,您将收到一个服务实例。使用这个实例是你可以来回“交谈”的方式。不幸的是,这是一个类,我们不能使用ServiceConnection,因为我们还没有了解它!有没有办法只用一个处理程序和线程就可以做到这一点呢?我基本上是在PrimeService.java的内部类中提供代码。我无法让处理程序通信,因为它们有两个单独的消息队列!我知道这一定是可行的,但我能想到的唯一方法是使用意图发送消息。但这似乎毫无意义。。。还有,谢谢你的帮助!好吧,也许我可以使用ServiceConnection。与其坐在这里试图弄明白这一点,不如使用一些有效的方法,并且可能会因此失去一些分数。谢谢如何从活动中停止内部类中的服务如何从活动中停止内部类中的服务
<service android:name=".outclass$innerService"/>