안드로이드 개발 질문/답변
(글 수 45,052)
/*
* Copyright (C) 2009 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.hellojni;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class HelloJni extends Activity {
private ListView lvResult;
Intent intentStartService;
static final private int CMD_FILE_READ = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lvResult = (ListView)findViewById(R.id.lvResult);
Button btSvcStart = (Button) findViewById(R.id.btSvcStart);
Button btSvcStop = (Button) findViewById(R.id.btSvcStop);
Button btFileRead = (Button) findViewById(R.id.btFileRead);
Toast.makeText(HelloJni.this, stringFromJNI(), Toast.LENGTH_SHORT)
.show();
intentStartService = new Intent(this, HelloService.class);
btSvcStart.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View _v) {
// TODO Auto-generated method stub
Toast.makeText(HelloJni.this, "Service starting",
Toast.LENGTH_SHORT).show();
//Toast.makeText(HelloJni.this, startServiceJNI(), Toast.LENGTH_SHORT).show();
startService(intentStartService);
}
});
btSvcStop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View _v) {
// TODO Auto-generated method stub
Toast.makeText(HelloJni.this, "Service stopping",
Toast.LENGTH_SHORT).show();
stopService(intentStartService);
}
});
btFileRead.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View _v) {
// TODO Auto-generated method stub
Toast.makeText(HelloJni.this, sendCmdToServiceJNI(CMD_FILE_READ), Toast.LENGTH_SHORT).show();
}
});
}
public native String stringFromJNI();
public native String sendCmdToServiceJNI(int _cmd);
public native String startServiceJNI();
static {
System.loadLibrary("hello-jni");
}
}
package com.example.hellojni;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
public class HelloService extends Service {
@Override
public void onStart(Intent intent, int startId) {
// TODO Auto-generated method stub
System.loadLibrary("hello-jni");
Context context = getApplicationContext();
Toast.makeText(context, startServiceJNI(), Toast.LENGTH_SHORT).show();
// Toast.makeText(context, "JNI Calling", Toast.LENGTH_SHORT).show();
}
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return null;
}
public native String startServiceJNI();
}
Activity에서 JNI를 호출하면 정상적을 작동을 합니다.
그러나 이상하게... Service에서 수행하면...
02-18 08:21:21.955: WARN/dalvikvm(307): No implementation found for native Lcom/example/hellojni/HelloService;.startServiceJNI ()Ljava/lang/String;
이런 메세지를 출력하면서 죽습니다.
물론 Activity에 설정한 코드는 Service에도 했습니다.
public native String startServiceJNI();,
static {
System.loadLibrary("hello-jni");
}
뭐가 문제일까요? NDK를 쓰기위해 설정하는 곳이 또 있나요?
==> 잘 몰라서 소스를 올립니다. 회색님 말씀대로 될 것 같은데... T.T
==> 윗 소스는 Activity고 아래는 Service입니다.
==> 잘 몰라서 소스를 올립니다. 회색님 말씀대로 될 것 같은데... T.T
==> 윗 소스는 Activity고 아래는 Service입니다.



