gpt4 book ai didi

android - 无法在内部类中启动服务 - 这是我的 Android list 吗?

转载 作者:行者123 更新时间:2023-11-29 15:27:17 26 4
gpt4 key购买 nike

求助!我在这里很绝望!!!!

我在启动服务时遇到问题。我将它移到了我的 Activity 中的内部类,但我无法启动它!

我不知道我是否必须更改我的 list 中的某些内容或以不同的方式命名它。

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

05-24 13:15:22.662: W/ActivityManager(80): 无法启动服务 Intent { 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 list :

<?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 中的一些代码...现在我只是试图从线程(在同一类中)获取我的字符串。我之前让它工作,但这次不工作。

这是我的新代码:

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();
}
}

最佳答案

您的服务 必须是静态类。然后一旦在您的 list 中,您将需要这样显示它

<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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com