实现简单的apk,实现对对应节点的访问
此次使用apk实现对相应节点的访问。
实现apk,主要分为两个部分:实现相应的apk主体部分,编译生成apk
一、实现相应的apk主体部分
由于本人对Java不是很熟悉,过借鉴了老罗的代码,不同之处此次使用的Android studio来新建一个apk工程取名为welcome,选择Android6.0的sdk。
1、主程序 welcome.java
生成的代码需要修改相应的import 模块跟代码,以便使用mtk代码,同时由于引用了自己的代码故在Android studio 中编译会报错
package com.example.atlas.welcome; //import com.example.atlas.welcome.R; //import android.support.v7.app.AppCompatActivity; import android.app.Activity; import android.os.Bundle; import android.os.ServiceManager; import android.os.IWelcomeService; import android.os.RemoteException; import android.util.Log; import android.util.EventLog; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.textservice.TextServicesManager; import android.widget.Button; import android.widget.EditText; import java.io.IOException; import java.util.ArrayList; import android.app.ListActivity; import android.content.Context; import android.content.pm.ApplicationInfo; import android.content.pm.PackageManager; import android.content.pm.PackageManager.NameNotFoundException; import android.graphics.drawable.Drawable; public class welcome extends Activity implements OnClickListener { private final static String LOG_TAG = "atlas.welcome"; private IWelcomeService welcomeService = null; private EditText valueText = null; private Button readButton = null; private Button writeButton = null; private Button clearButton = null; // called when the activity is first created. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_welcome); welcomeService = IWelcomeService.Stub.asInterface(ServiceManager.getService("welcome")); valueText = (EditText)findViewById(R.id.edit_value); readButton = (Button)findViewById(R.id.button_read); writeButton = (Button)findViewById(R.id.button_write); clearButton = (Button)findViewById(R.id.button_clear); readButton.setOnClickListener(this); writeButton.setOnClickListener(this); clearButton.setOnClickListener(this); Log.i(LOG_TAG,"Welcome Activity Created"); } @Override public void onClick(View v){ if(v.equals(readButton)){ try{ int val = welcomeService.getVal(); String text = String.valueOf(val); valueText.setText(text); }catch (RemoteException e){ Log.e(LOG_TAG,"Remote Exception while reading value from device."); } }else if(v.equals(writeButton)){ try{ String text = valueText.getText().toString(); int val = Integer.parseInt(text); welcomeService.setVal(val); }catch (RemoteException e){ Log.e(LOG_TAG,"Remote Exception while writing value to device."); } }else if(v.equals(clearButton)){ String text = ""; valueText.setText(text); } } }
1.2 修改layout 目录下的activity_welcome.xml 文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/value"> </TextView> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/edit_value" android:hint="@string/hint"> </EditText> </LinearLayout> <LinearLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:gravity="center"> <Button android:id="@+id/button_read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/read"> </Button> <Button android:id="@+id/button_write" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/write"> </Button> <Button android:id="@+id/button_clear" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/clear"> </Button> </LinearLayout> </LinearLayout>
1.3、 字符串文件res/values/strings.xml
<resources> <string name="app_name">welcome</string> <string name="value">Value</string> <string name="hint">Please input a value...</string> <string name="read">Read</string> <string name="write">Write</string> <string name="clear">Clear</string> </resources>
1.4 修改styles.xml
<?xml version="1.0" encoding="utf-8"?> <!-- /* * Copyright (C) 2010, 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. */ --> <resources> <style name="TallTitleBarTheme" parent="android:Theme.NoTitleBar"> <item name="android:windowContentOverlay">@null</item> </style> </resources>
1.5 程序描述文件AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.atlas.welcome" android:sharedUserId="android.uid.log" > <application android:label="welcome" > <activity android:name=".welcome" android:label="welcome" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
二、编译生成apk
2.1、在适当的位置建立app目录,此次选择packagesapps建立welcome目录。
将Android studio 建立的源文件目录 welcomeappsrcmain 下的的文件copy到welcome目录下。
2.2、添加Android.mk 文件,添加编译规则
LOCAL_PATH:= $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE_TAGS:=optional LOCAL_SRC_FILES := $(call all-java-files-under, java) LOCAL_PACKAGE_NAME:=welcome LOCAL_CERTIFICATE := platform include $(BUILD_PACKAGE) #include $(call all-makefiles-under,$(LOCAL_PATH))
至此apk代码部分完成。
三、编译,下载,验证
使用mmm package/apps/welcome 编译即可,打包下载或者直接安装。
如有相关权限问题,添加对应的权限即可。至此,从kernel 到apk部分代码完成。