助けてください!私はここで必死です!!!!
サービスの開始に問題があります。アクティビティ内の内部クラスに移動しましたが、開始できません!
マニフェストで何かを変更する必要があるのか 、別の方法で呼び出す必要があるのか わかりません。
「Intent intent = new Intent(HW07.this, PrimeService.class);」で呼び出してみた 次のエラーが発生しました。
05-24 13:15:22.662: W/ActivityManager(80): サービスの意図を開始できません
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 のいくつかのコードを変更しました...今、スレッドから (同じクラス内で) 文字列を取得しようとしています。以前は動作しましたが、今回は動作しません。
ここに私の新しいコードがあります:
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();
}
}