AndroidはJNIを使ってC/C++を呼び出して生成した.soライブラリの流れ(機能:appkがアンインストールされているときはウェブページを呼び出す)

19404 ワード

注:機能は、appkがアンマウントされたときにアンインストールされたフィードバックのウェブページを開きます。ここで、linuxのCコードは、Linuxから発生します。http://www.cnblogs.com/zealotrouge/p/3182617.html本論文の流れは主に生成、soプロセス、ロード使用などです。
(コア機能コードの著作権はhttp://www.cnblogs.com/zealotrouge/の熱気球が全部あります。少しだけ変えます。
注2:cygwin、NDKなどの他の配置はgoogleに教えてください。
1.自分のアプリで、アンマウントを検出するためのUnistallStatictics.javaクラスを生成します。
package jp.accessport.gamebox.utils;

import java.lang.reflect.Method;

import android.content.Context;
import android.os.Build;
import android.util.Log;

//      ==  

public class UninstallStatictics {

	//log
	private static final String	TAG = "UninstallStatictics";
	//         
	private static final String	S_ObserverLibrary = "observe-uninstall";
	
	//    pid
	private int	m_nObserverProcessPid = -1;
	
	//       :      
	private native int	initObserverProcess(String strUserSerial);
	
	//
	static {
		Log.d(TAG, "load observer process lib --> " + S_ObserverLibrary);
		System.loadLibrary(S_ObserverLibrary);
	}
	
	
	//     ,        
	public void start(Context context){
		//API level  17,     userSerialNumber
		if (Build.VERSION.SDK_INT < 17)
			m_nObserverProcessPid = initObserverProcess(null);
		//  ,    userSerialNumber
		else
			m_nObserverProcessPid = initObserverProcess(getUserSerial(context));
	}
	
	
	//  targetSdkVersion  17,        
	//      :<uses-permission android:name="android.permission.READ_PHONE_STATE" />
	private String getUserSerial(Context context){
		Object	objUserManager = context.getSystemService("user");
		if (objUserManager == null){
			Log.e(TAG, "userManager not exist!!!");
			return null;
		}

		String	strSN = null;
		try {
			Class<?>	classSP = Class.forName("android.os.SystemProperties");
			Method		methodGet = classSP.getMethod("get", String.class);
			strSN = (String)methodGet.invoke(classSP, "ro.serialno");
		}
		catch (Exception e){
			e.printStackTrace();
		}
		
		return strSN;
	}
}
2.開発環境はWindows、eclipseなので、eclipseの工程で更新すれば対応できます。classファイル(ここはUnistallStatictics.class)は、最初は生成のためです。classはjavacを使っています。最後はまだ生成されていません。android、java共用の場合は発生が面倒くさいと言われています。javaではないです。いくつかの命令==まだ未熟です。
3.javahコマンドを使ってjp_を生成する。accessportgamebox_utils_UnistallStatictics.hヘッダファイル(ネーミング=ルールがありますので、自分の名前を表示する必要はありません)は、簡単なタイプではなく、Conteext==のようなandroidのクラスが含まれていますので、生成過程で様々な問題が発生します。解決方法は参照してください。http://blog.csdn.net/hejinjing_トム.com/アート/detail/8125648
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>

/* Header for class jp_accessport_gamebox_utils_UninstallStatictics */

#ifndef _Included_jp_accessport_gamebox_utils_UninstallStatictics
#define _Included_jp_accessport_gamebox_utils_UninstallStatictics
#ifdef __cplusplus
extern "C" {
#endif

/*
 * Class:     jp_accessport_gamebox_utils_UninstallStatictics
 * Method:    initObserverProcess
 * Signature: (Ljava/lang/String;)I
 */
JNIEXPORT jint JNICALL Java_jp_accessport_gamebox_utils_UninstallStatictics_initObserverProcess
  (JNIEnv *, jobject, jstring);

#ifdef __cplusplus
}
#endif
#endif
4.生成.hファイル後、コードを書いてJava_を実現することができます。jp_accessportgamebox_utils_ユニスタースタティックシティinitObserver Process機能は、私が生成したクラスの実現には対応していません。hファイルの名前は、生成後は必要ありません。hファイルはあります。soの実現があればいいです。内部の関数名は自由ですが、これはnativeの名前と関連しています。
// :    .so   ,         jp_accessport_gamebox_utils_UninstallStatictics.h   
//       #include "jp_accessport_gamebox_utils_UninstallStatictics.h"
#include <jni.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/inotify.h>
#include <android/log.h>

//log   
//#define LOG_INFO(tag, msg)		__android_log_write(ANDROID_LOG_INFO, tag, msg)
//#define LOG_DEBUG(tag, msg)		__android_log_write(ANDROID_LOG_DEBUG, tag, msg)
//#define LOG_WARNING(tag, msg)	__android_log_write(ANDROID_LOG_WARNING, tag, msg)
//#define LOG_ERROR(tag, msg)		__android_log_write(ANDROID_LOG_ERROR, tag, msg)

#undef jp_accessport_gamebox_utils_UninstallStatictics_MODE_PRIVATE
#define jp_accessport_gamebox_utils_UninstallStatictics_MODE_PRIVATE 0L
#undef jp_accessport_gamebox_utils_UninstallStatictics_MODE_WORLD_READABLE
#define jp_accessport_gamebox_utils_UninstallStatictics_MODE_WORLD_READABLE 1L
#undef jp_accessport_gamebox_utils_UninstallStatictics_MODE_WORLD_WRITEABLE
#define jp_accessport_gamebox_utils_UninstallStatictics_MODE_WORLD_WRITEABLE 2L
#undef jp_accessport_gamebox_utils_UninstallStatictics_MODE_APPEND
#define jp_accessport_gamebox_utils_UninstallStatictics_MODE_APPEND 32768L
#undef jp_accessport_gamebox_utils_UninstallStatictics_MODE_MULTI_PROCESS
#define jp_accessport_gamebox_utils_UninstallStatictics_MODE_MULTI_PROCESS 4L
#undef jp_accessport_gamebox_utils_UninstallStatictics_BIND_AUTO_CREATE
#define jp_accessport_gamebox_utils_UninstallStatictics_BIND_AUTO_CREATE 1L
#undef jp_accessport_gamebox_utils_UninstallStatictics_BIND_DEBUG_UNBIND
#define jp_accessport_gamebox_utils_UninstallStatictics_BIND_DEBUG_UNBIND 2L
#undef jp_accessport_gamebox_utils_UninstallStatictics_BIND_NOT_FOREGROUND
#define jp_accessport_gamebox_utils_UninstallStatictics_BIND_NOT_FOREGROUND 4L
#undef jp_accessport_gamebox_utils_UninstallStatictics_BIND_ABOVE_CLIENT
#define jp_accessport_gamebox_utils_UninstallStatictics_BIND_ABOVE_CLIENT 8L
#undef jp_accessport_gamebox_utils_UninstallStatictics_BIND_ALLOW_OOM_MANAGEMENT
#define jp_accessport_gamebox_utils_UninstallStatictics_BIND_ALLOW_OOM_MANAGEMENT 16L
#undef jp_accessport_gamebox_utils_UninstallStatictics_BIND_WAIVE_PRIORITY
#define jp_accessport_gamebox_utils_UninstallStatictics_BIND_WAIVE_PRIORITY 32L
#undef jp_accessport_gamebox_utils_UninstallStatictics_BIND_IMPORTANT
#define jp_accessport_gamebox_utils_UninstallStatictics_BIND_IMPORTANT 64L
#undef jp_accessport_gamebox_utils_UninstallStatictics_BIND_ADJUST_WITH_ACTIVITY
#define jp_accessport_gamebox_utils_UninstallStatictics_BIND_ADJUST_WITH_ACTIVITY 128L
#undef jp_accessport_gamebox_utils_UninstallStatictics_CONTEXT_INCLUDE_CODE
#define jp_accessport_gamebox_utils_UninstallStatictics_CONTEXT_INCLUDE_CODE 1L
#undef jp_accessport_gamebox_utils_UninstallStatictics_CONTEXT_IGNORE_SECURITY
#define jp_accessport_gamebox_utils_UninstallStatictics_CONTEXT_IGNORE_SECURITY 2L
#undef jp_accessport_gamebox_utils_UninstallStatictics_CONTEXT_RESTRICTED
#define jp_accessport_gamebox_utils_UninstallStatictics_CONTEXT_RESTRICTED 4L
#undef jp_accessport_gamebox_utils_UninstallStatictics_RESULT_CANCELED
#define jp_accessport_gamebox_utils_UninstallStatictics_RESULT_CANCELED 0L
#undef jp_accessport_gamebox_utils_UninstallStatictics_RESULT_OK
#define jp_accessport_gamebox_utils_UninstallStatictics_RESULT_OK -1L
#undef jp_accessport_gamebox_utils_UninstallStatictics_RESULT_FIRST_USER
#define jp_accessport_gamebox_utils_UninstallStatictics_RESULT_FIRST_USER 1L
#undef jp_accessport_gamebox_utils_UninstallStatictics_DEFAULT_KEYS_DISABLE
#define jp_accessport_gamebox_utils_UninstallStatictics_DEFAULT_KEYS_DISABLE 0L
#undef jp_accessport_gamebox_utils_UninstallStatictics_DEFAULT_KEYS_DIALER
#define jp_accessport_gamebox_utils_UninstallStatictics_DEFAULT_KEYS_DIALER 1L
#undef jp_accessport_gamebox_utils_UninstallStatictics_DEFAULT_KEYS_SHORTCUT
#define jp_accessport_gamebox_utils_UninstallStatictics_DEFAULT_KEYS_SHORTCUT 2L
#undef jp_accessport_gamebox_utils_UninstallStatictics_DEFAULT_KEYS_SEARCH_LOCAL
#define jp_accessport_gamebox_utils_UninstallStatictics_DEFAULT_KEYS_SEARCH_LOCAL 3L
#undef jp_accessport_gamebox_utils_UninstallStatictics_DEFAULT_KEYS_SEARCH_GLOBAL
#define jp_accessport_gamebox_utils_UninstallStatictics_DEFAULT_KEYS_SEARCH_GLOBAL 4L

#ifdef __cplusplus
extern "C" {
#endif


//    
static char		TAG[] = "UninstallStatictics.initObserverProcess";
static jboolean isCopy = JNI_TRUE;

static const char	APP_DIR[] = "/data/data/jp.accessport.gamebox";
static const char	APP_FILES_DIR[] = "/data/data/jp.accessport.gamebox/files";
static const char	APP_OBSERVED_FILE[] = "/data/data/jp.accessport.gamebox/files/observedFile";
static const char	APP_LOCK_FILE[] = "/data/data/jp.accessport.gamebox/files/lockFile";

/************************************************************************/
/* class:	jp_accessport_gamebox_utils_UninstallStatictics
/* method:	initObserverProcess
/* return:	   pid
/************************************************************************/
JNIEXPORT jint JNICALL Java_jp_accessport_gamebox_utils_UninstallStatictics_initObserverProcess(JNIEnv *env, jobject obj, jstring userSerial)
{
	jstring	tag = (*env)->NewStringUTF(env, TAG);
//	LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &isCopy)
//				, (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "init observer"), &isCopy));

	//fork   ,       
	pid_t	pid = fork();
	if (pid < 0)
	{
//		LOG_ERROR((*env)->GetStringUTFChars(env, tag, &isCopy)
//					, (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "fork failed !!!"), &isCopy));
		exit(1);
	}
	else if (pid == 0)
	{
		//             ,   
		FILE*	pFilesDir = fopen(APP_FILES_DIR, "r");
		if (pFilesDir == NULL)
		{
			int	nRet = mkdir(APP_FILES_DIR, S_IRWXU | S_IRWXG | S_IXOTH);
			if (nRet == -1)
			{
//				LOG_ERROR((*env)->GetStringUTFChars(env, tag, &isCopy)
//							, (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "mkdir failed !!!"), &isCopy));
				exit(1);
			}
		}
		fclose(pFilesDir);

		//         ,     
		FILE*	pObservedFile = fopen(APP_OBSERVED_FILE, "r");
		if (pObservedFile == NULL)
			pObservedFile = fopen(APP_OBSERVED_FILE, "w");
		fclose(pObservedFile);

		//     ,                   
		int		nLockFileDescriptor = open(APP_LOCK_FILE, O_RDONLY);
		if (nLockFileDescriptor == -1)
			nLockFileDescriptor = open(APP_LOCK_FILE, O_CREAT);
		int		nLockRet = flock(nLockFileDescriptor, LOCK_EX | LOCK_NB);
		if (nLockRet == -1)
		{
//			LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &isCopy)
//						, (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "observed by another process"), &isCopy));
			exit(0);
		}
//		LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &isCopy)
//					, (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "observed by child process"), &isCopy));

		//    ,    event
		void*	pBuf = malloc(sizeof(struct inotify_event));
		if (pBuf == NULL)
		{
//			LOG_ERROR((*env)->GetStringUTFChars(env, tag, &isCopy)
//						, (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "malloc failed !!!"), &isCopy));
			exit(1);
		}

		//    ,    mask
		int		nMaskStringLen = 7 + 10 + 1;	//mask=0x 7  ,32       10 ,       10  ,'\0' 1  
		char*	pMaskString = malloc(nMaskStringLen);
		if (pMaskString == NULL)
		{
			if (pBuf != NULL)
			{
				free(pBuf);
				pBuf = NULL;
			}
//			LOG_ERROR((*env)->GetStringUTFChars(env, tag, &isCopy)
//						, (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "malloc failed !!!"), &isCopy));
			exit(1);
		}

		//    
//		LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &isCopy)
//					, (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "start observe"), &isCopy));

		//   
		int		nFileDescriptor = inotify_init();
		if (nFileDescriptor < 0)
		{
			if (pMaskString != NULL)
			{
				free(pMaskString);
				pMaskString = NULL;
			}
			if (pBuf != NULL)
			{
				free(pBuf);
				pBuf = NULL;
			}

//			LOG_ERROR((*env)->GetStringUTFChars(env, tag, &isCopy)
//						, (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "inotify_init failed !!!"), &isCopy));
			exit(1);
		}

		//            
		int		nWatchDescriptor = inotify_add_watch(nFileDescriptor, APP_OBSERVED_FILE, IN_ALL_EVENTS);
		if (nWatchDescriptor < 0)
		{
			if (pMaskString != NULL)
			{
				free(pMaskString);
				pMaskString = NULL;
			}
			if (pBuf != NULL)
			{
				free(pBuf);
				pBuf = NULL;
			}

//			LOG_ERROR((*env)->GetStringUTFChars(env, tag, &isCopy)
//						, (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "inotify_add_watch failed !!!"), &isCopy));
			exit(1);
		}

		//
		while (1)
		{
			//read     
			size_t	nReadBytes = read(nFileDescriptor, pBuf, sizeof(struct inotify_event));

			//  mask
			snprintf(pMaskString, nMaskStringLen, "mask=0x%x\0", ((struct inotify_event *)pBuf)->mask);

//			LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &isCopy)
//						, (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, pMaskString), &isCopy));

			//      ,      ,       app      
			if (IN_DELETE_SELF == ((struct inotify_event *)pBuf)->mask)
			{
				FILE*	pAppDir = fopen(APP_DIR, "r");
				//     
				if (pAppDir == NULL)
				{
					inotify_rm_watch(nFileDescriptor, nWatchDescriptor);
					break;
				}
				//   
				else
				{
					fclose(pAppDir);

					//        ,     
					FILE*	pObservedFile = fopen(APP_OBSERVED_FILE, "w");
					fclose(pObservedFile);

					nWatchDescriptor = inotify_add_watch(nFileDescriptor, APP_OBSERVED_FILE, IN_ALL_EVENTS);
					if (nWatchDescriptor < 0)
					{
						if (pMaskString != NULL)
						{
							free(pMaskString);
							pMaskString = NULL;
						}
						if (pBuf != NULL)
						{
							free(pBuf);
							pBuf = NULL;
						}

//						LOG_ERROR((*env)->GetStringUTFChars(env, tag, &isCopy)
//									, (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "inotify_add_watch failed !!!"), &isCopy));
						exit(1);
					}
				}
			}
		}

		//    
		if (pMaskString != NULL)
		{
			free(pMaskString);
			pMaskString = NULL;
		}
		if (pBuf != NULL)
		{
			free(pBuf);
			pBuf = NULL;
		}

		//    
//		LOG_DEBUG((*env)->GetStringUTFChars(env, tag, &isCopy)
//					, (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "stop observe"), &isCopy));

		if (userSerial == NULL)
		{
			//     am start -a android.intent.action.VIEW -d $(url)
			execlp("am", "am", "start", "-a", "android.intent.action.VIEW", "-d", "http://www.so.com", (char *)NULL);
		}
		else
		{
			//     am start --user userSerial -a android.intent.action.VIEW -d $(url)
			execlp("am", "am", "start", "--user", (*env)->GetStringUTFChars(env, userSerial, &isCopy), "-a", "android.intent.action.VIEW", "-d", "http://www.so.com", (char *)NULL);
		}

		//       log
//		LOG_ERROR((*env)->GetStringUTFChars(env, tag, &isCopy)
//					, (*env)->GetStringUTFChars(env, (*env)->NewStringUTF(env, "exec AM command failed !!!"), &isCopy));
	}
	else
	{
		//        ,     init    ,        ,       pid
		return pid;
	}
}


#ifdef __cplusplus
}
#endif
5.JNIに呼び出させるライブラリファイルを生成するために、D:\PrograamFiles\android-ndk-r 8 e\samples\test-jniディレクトリの下(本人のndkインストールディレクトリ)に、test-jniプロジェクトtest-jni 2をコピーして、test-jni 2\jdroidファイルの下に修正します。
# 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.
#
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := observe-uninstall	#   .so     ,    
LOCAL_SRC_FILES := observe-file.c	#       ,                        

#       ,  observe-file.c       linux  ,      ,   cygwin      ,   ,           ...
LOCAL_C_INCLUDES := $(LOCAL_PATH)/../../../platforms/android-14/arch-arm/usr/include

LOCAL_EXPORT_LDLIBS:= -llog		#         ,        .so   

include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS) 
私は元々windowsの下のC++で、linuxのCプログラムは本当に不慣れなので、オンラインでアンディのJNIについて呼び出すことができます。dllファイルを見ましたが、考えた後、windowsの下のapiが実現すると感じました。linuxを基礎としたandroidシステムでは機能しないと予想されます。最後は硬い頭皮でlinuxの下を整えました。そして。
また、個人の感覚では、Androidは起動できます。dllは主に自分の機能の実現を指します。システムに依存しないでください。さもなければ、呼び出しは成功しないと推定されます。
他の2:どうすればいいか分かりませんでした。ネット上の教程を探しましたが、コンパイルができないものもあります。cococos 2 d-xも実験しました。最後に問題があります。
6.cygwinを使用して、cdからtest-jni 2ディレクトリに:cd/cygdrive/D/Program Files/android-ndk-r 8 e/samples/test-jni 2/を使用してコマンドを使用します。ROOT/ndk-buildでコンパイルできます。
注1:NDK_ROOTはcygwinのインストールディレクトリの下でD:\Programe Files\cygwin\home\gxj\.bash_profileファイルの最後に追加されたndkルートディレクトリを実行する変数名は、下記のコードの一番下の2行の名前のように、自分でも修正できます。
# To the extent possible under law, the author(s) have dedicated all 
# copyright and related and neighboring rights to this software to the 
# public domain worldwide. This software is distributed without any warranty. 
# You should have received a copy of the CC0 Public Domain Dedication along 
# with this software. 
# If not, see <http://creativecommons.org/publicdomain/zero/1.0/>. 

# base-files version 4.1-1

# ~/.bash_profile: executed by bash(1) for login shells.

# The latest version as installed by the Cygwin Setup program can
# always be found at /etc/defaults/etc/skel/.bash_profile

# Modifying /etc/skel/.bash_profile directly will prevent
# setup from updating it.

# The copy in your home directory (~/.bash_profile) is yours, please
# feel free to customise it to create a shell
# environment to your liking.  If you feel a change
# would be benifitial to all, please feel free to send
# a patch to the cygwin mailing list.

# User dependent .bash_profile file

# source the users bashrc if it exists
if [ -f "${HOME}/.bashrc" ] ; then
  source "${HOME}/.bashrc"
fi

# Set PATH so it includes user's private bin if it exists
# if [ -d "${HOME}/bin" ] ; then
#   PATH="${HOME}/bin:${PATH}"
# fi

# Set MANPATH so it includes users' private man if it exists
# if [ -d "${HOME}/man" ]; then
#   MANPATH="${HOME}/man:${MANPATH}"
# fi

# Set INFOPATH so it includes users' private info if it exists
# if [ -d "${HOME}/info" ]; then
#   INFOPATH="${HOME}/info:${INFOPATH}"
# fi

NDK_ROOT=/cygdrive/D/ProgramFiles/android-ndk-r8e
export NDK_ROOT
注2:コンパイルコマンドを使う時は「NDKuROOT/ndk-build」ではなく「NDKuROOT/ndk-build」を使います。コンパイルする前にデフォルトの記号がありますので、コンパイルコマンドは入力しなくてもいいと思いました。何回やっても成功しませんでした。最後にcopyを入れてコマンド「NDKubrond/有効です。」
7.コンパイルに成功した後、D:\PrograamFiles\android-ndk-r 8 e\samples\test-jni 2\libs\armeabi\libobserven-uninstall.soファイルを生成し、自分のappkプロジェクトディレクトリE:Company Cloriend\ap_gamebox\libs\armeabi\下に直接にLibsの下に置かないように注意してください。libs\armeabi\下に置いてください。そうしないと包装できません。
また、作成した.soの名前はlib+自分の名前ですが、androidで呼び出した時にSystem.loadLibraryを使う時は、前のlibの後ろのサフィックスを持たないでください。そして、System.loadLibraryです。もちろんSystem.loadを使うなら、フルパスが必要なようです。名前もフルネームでお願いします。
8.コンパイル.appプロジェクトを更新し、テストすると、自分のアプリを起動した後、appkをアンインストールするとウェブページwwww.so.comがポップアップします。以上