2015年1月14日 星期三

[Android] 基礎Google Map教學

終於把這個很煩人的google map弄出一點樣子

寫在這邊紀錄一下

可能會產生的錯誤會有幾種

1)  INSTALL_FAILED_MISSING_SHARED_LIBRARY Error

2) API Key failed

3) Permission denied



第一種很顯然你刷機(flash ROM)時候法google libary沒有匯入

第二種則是API Key你沒有用你專屬的key ->請到 https://console.developers.google.com/ 申請一

個,申請詳細辦法






切記!!! 你的package name要正確,不正確會無法顯示地圖!!

就把SHA1;package.name輸入在空格中,建立,然後就有API key了

第三種解決方式就是檢查你的AndroidManifest.xml是否有加入適當的permission


-------------------------------------------------------------------------------------------------------------------


以下為三項基礎

1.google play library

首先開啟SDK Manager ->檢查google play service (以下用圖來表示)








有錯誤一定是沒有import正確 / 沒有加入external jar files

然後



這部分需要開啟權限




最後原本的網址載點在這邊

https://github.com/googlemaps/hellomap-android

程式碼跟layout可以上去下載來看看

layout:

<?xml version="1.0" encoding="utf-8"?>
<!--
See this page for more XML attribute options
https://developers.google.com/maps/documentation/android/map#using_xml_attributes
-->
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:map="http://schemas.android.com/apk/res-auto"
          android:id="@+id/map"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:name="com.google.android.gms.maps.SupportMapFragment"
          map:mapType="normal"/>




MainActivity:

package com.example.hellomap;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;

public class MainActivity extends FragmentActivity {
    private GoogleMap mMap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}


就這樣! 

完成圖 End

2015年1月8日 星期四

[Android] 長按切換2個Activity-----intent.setClass錯誤排除-----onGestureListener監聽手勢


為了讓兩個activity能夠順利切換,花了一些時間研究,

當然中間也遇到一些問題,在這邊紀錄一下.


首先切換Activity是使用intent->startActivity()來做切換

觸發則是長按畫面任意地方


首先先看如何監聽手勢(gesture):

OnGestureListener listener = new OnGestureListener(){

@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2,
float velocityX, float velocityY) {
// TODO Auto-generated method stub
return false;
}

@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
       Intent intent = new Intent();
       intent.setClass(MainActivity.this, switch_target.class);
       startActivity(intent);
       overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
       MainActivity.this.finish();
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
// TODO Auto-generated method stub
return false;
}

@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}};


別忘記要import 所需要的東西

import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;



動畫的部分,首先要在res/anim裡面新增兩個xml檔

第一個檔,檔名隨意設定
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:anim/linear_interpolator"
        android:fromXScale="0.0"
        android:toXScale="1.0"
        android:fromYScale="0.7"
        android:toYScale="1.0"
        android:fillAfter="false"
        android:startOffset="200"
        android:duration="200" />
    <translate
        android:fromXDelta="50%"
        android:toXDelta="0"
        android:startOffset="200"
        android:duration="200"/>
</set>


第二個檔也是檔名隨意設定


有了動畫xml檔 + 手勢(觸發事件),我們還缺一樣東西,就是切換activity


       Intent intent = new Intent();
       intent.setClass(MainActivity.this, switch_target.class);
       startActivity(intent);
       overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
       MainActivity.this.finish();

切換Activity需要的就是intent,注意這邊

intent.setClass(MainActivity.this, switch_target.class);

是MainActivity.this, switch_target.class兩者是不一樣的,請不要搞錯了(搞錯他就會一直報錯)

 

The method setClass(Context, Class<?>) in the type Intent is not applicable for the arguments...............


這樣的錯誤訊息


另一端切換回來則是:

       Intent intent = new Intent();
       intent.setClass(switch_target.this,MainActivity.class);
       startActivity(intent);
       overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
       switch_target.this.finish();


當然為了避免還是有人看了以後還是出錯,這邊也附上source code 方便大家看

這次的教學就到此囉 End

2014年12月29日 星期一

[Android] No resource found that matches the given name '@style/Theme.AppCompat.Light' / 解決方法

剛開始安裝android / eclipse的時候通常都會遇到這個問題,

相信這個問題困擾許多人已久(包括我)...終於了解+找到解法以後把他寫起來以免下次又忘記!


首先,甚麼叫做IsLibrary?

這個選項打勾就代表說你把該專案當成library(所以不能亂勾,雖然勾了也沒太大影響(?))


Step1:
加入library(File->Import)



選擇sdk-extras-android-support-v7-appcompat資料夾,記得要勾選copy to workspace

Step2:

確認專案->右鍵Properties->anddroid(左側)->Library(isLibrary)是否勾選


Step3:

進到目標Project(就是有錯誤的那個project)

同樣也是properties->android->library->把錯誤的remove掉



add->剛剛新建立的android-support-v7-appcompat->OK (這邊請不要勾選is library)


Step4:

又發現錯誤!!



在builders這邊也有錯所以我們在Builders->Libraries->選擇錯誤的項目->edit->選圖片內的路徑

下面這個也有錯,所以也修正過來

Step5:

完成! 看到style.xml這邊完全沒有錯誤了,project也是沒有驚嘆號出現


就是這樣!幾個步驟做完就可以囉!

如果還是無法記得要去SDK Manager下載新的extras(要按install)

END


2014年12月23日 星期二

[Android] 如何修改Android內Icon

往往製作App的最後一個步驟就是要製作一個Icon,

製作Icon非難事,illustrator即可,甚至icon maker也可以輕意辦到。

但如何把修改好的(製作好的)Icon放進去Eclipse呢?

在專案中找到

1.AndroidManifest.xml

2.點開裡面->Icon->Browse...

3.create new icon..

製作好以後(一直next),選擇你製作好的icon即可。

重新build後,就可以拉!

End.

2014年11月28日 星期五

[Android] 寫入/讀取檔案(內部記憶體)

其實寫入或讀取內部/外部記憶體只差在一個要增加權限(androidmanifest.xml)一個不用而已,

所以如果你是要寫入sd卡,還是可以看一下這篇:)


寫入的部分:

//寫入檔案
private String filename = "MySampleFile.txt";
private String filepath = "MyFileStorage";
File myInternalFile;

.............以上為宣告...................

// 目前時間
Date date = new Date();
// 設定日期格式
SimpleDateFormat sdf = new SimpleDateFormat("MM-dd HH:mm:ss");
// 進行轉換
String dateString = sdf.format(date);

try {
ContextWrapper contextWrapper = new ContextWrapper(getApplicationContext());
File directory = contextWrapper.getDir(filepath,MODE_APPEND);// 不存在就創建,存在就追加
myInternalFile = new File(directory, filename);
FileOutputStream fos = new FileOutputStream(myInternalFile,true);//要加上true才會是append否則預設false
String s = dateString + "," + event+"\n";// +事件
fos.write(s.getBytes());
fos.close();
Log.d("foutstream", "stream");
} catch (IOException e) {
e.printStackTrace();
}

看起來十分簡單,實際運作也十分簡單。

建立一個檔案,這邊記得要用MODE_APPEND(不存在就創建檔案,存在就繼續寫下去),如果用MODE_PRIVATE會蓋掉喔!

然後在FileOutputStream這邊預設是false(append false),因此要改成
FileOutputStream fos = new FileOutputStream(myInternalFile,true);
這樣才能成功複寫。



讀取的部分:

String myData = "";
try {
FileInputStream fis = new FileInputStream(myInternalFile);
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
myData = myData + strLine + "\n";
}
in.close();
                }catch(Exception e){}


就是用FileInputStream讀取,然後每行每行讀出來,讀到沒有為止,最後記得一樣要關stream!



十分的簡單,但也花了我不少時間,記錄一下。END