這一段應學妹要求,做了一個關於物聯網的wifi robot的安卓端應用,其中很多的代碼段都是參考網上的,沒有什麼原創性可言,這裏我就發一些自己遇到的問題,給大家一點借鑑。
應用功能是 首先一個歡迎界面,幾秒鐘後跳轉到狀態顯示頁面,顯示下位機傳來的溫度,PH等數值,左右滑動進入視頻顯示界面,並有舵機的控制按鍵。
其中wifi的刷機教程網上有很多,這裏不贅述。
遇見的第一個問題:界面跳轉時自動彈出->原因:沒有在AndroidMainfest.xml中添加自定義的Activitiy,紅色爲自定義的部分。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lessonone"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.lessonone.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<span style="color:#ff0000;"><activity
android:name=".statusActivity"
>
</activity>
<activity
android:name=".controlActivity"
>
</activity></span>
</application>
</manifest>
第二個問題:反編譯的解讀問題:
因爲參考了一個APP,遇到了一些反編譯的問題。
1.像類似這種語句,其內部的參數可以在R中查找。
super.onCreate(paramBundle);
setContentView(2130903042);
2.這裏有無用的局部變量
TextView localTextView1 = (TextView)findViewById(2131099659);
this.tempTextView = localTextView1;
可以改寫爲tempTextView=(TextView)findViewById(2131099659);
其中tempTextView爲自定義的本地變量,數字是ID號。
3.內部方法
package dennis.android;
import android.view.View;
import android.view.View.OnClickListener;
import dalvik.annotation.EnclosingMethod;
@EnclosingMethod
class ControlActivity$7
implements View.OnClickListener
{
public void onClick(View paramView)
{
SocketThread localSocketThread = this.this$0.socketThread;
byte[] arrayOfByte = ControlActivity.access$6(this.this$0);
localSocketThread.send(arrayOfByte);
}
}
經常有人問到EnclosingMethod 的問題,網上的解答也不多。這裏會在左邊的項目欄中出現一個新的Activity,其實是一個內部的方法調用。
這裏的access指的是其他類的外部方法。實際的代碼差不多如下:
rightButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
byte[] ArrayofByte = CMD_RIGHT;
// TODO Auto-generated method stub
socketThread.send(ArrayofByte);
}
});
第三個問題:其實這個就顯得我有些弱智了。記得在自定義的surfaceView類裏添加while(ture)保證畫面能刷新。。。,其中醜陋的變量部分請忽略。
<span style="color:#ff6666;">while(true)</span>
{
try {
videoUrl=new URL(url);
conn=(HttpURLConnection)videoUrl.openConnection();
conn.setDoInput(true);
conn.connect();
inputStream=conn.getInputStream();
bmpBitmap=BitmapFactory.decodeStream(inputStream);
Bitmap localBitmap=bmpBitmap;
int i=ScreenW;
int j=ScreenH;
Bitmap dstmBitmap=Bitmap.createScaledBitmap(localBitmap, i, j,true);
bmpBitmap=dstmBitmap;
canvas=new Canvas(bmpBitmap);
canvas=surfaceHolder.lockCanvas();
canvas.drawBitmap(bmpBitmap, 0,0,null);
surfaceHolder.unlockCanvasAndPost(canvas);
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
第四個問題:使用自定義的surfaceview不顯示,會黑屏。記得在紅色部分換上自定義的類代替原來的控件。<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<<span style="color:#ff0000;">com.example.lessonone.MySurfaceView</span>
android:id="@+id/surfaceView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="35dp"
android:layout_marginLeft="53dp"
android:text="Button" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignParentRight="true"
android:layout_marginRight="38dp"
android:text="Button" />
</RelativeLayout>
最後一個問題:額,不用忘了入網許可,在第一個XML中有體現。
本人屬於菜鳥級別,有什麼說錯的地方,敬請指導。