티스토리 뷰

Study

Android(안드로이드) 관련

메디츠 2024. 2. 4. 08:56
반응형

14년 4월 기준.

 

Android에서 주소 자동링크 막기

<meta name="format-detection" content="telephone=no">

address=no, email=no

content=no

 

안드로이드 에뮬레이터 회전

CTRL + F11, CTRL + F12

 

현재 해상도 구하기

Display display = ((WindowManager)getSystemService(WINDOW_SERVICE)).getDefaultDisplay();

int width = display.getWidth();

int height = display.getHeight();

View에서는

int mWidth = context.getResources().getDisplayMetrics().widthPixels;

int mHeight = context.getResources().getDisplayMetrics().heightPixels;

 

안드로이드 종료

ActivityManager am = (ActivityManager)getSystemService(ACTIVITY_SERVICE);

am.restartPackage(getPackageName());

AndroidManifest.xml

<uses-permission android:name="android.permission.RESTART_PACKAGES"/>

 

moveTaskToBack(true);

finish();

 

System.exit(0);

 

Android Intent

Intent i = new Intent (this, InGame.class);

startActivity (i);

// Activity 전환

 

//Intent intent = new Intent (Intent.ACTION_PICK, Uri.parse ("content://contacts/people"));

//Intent intent = new Intent (Intent.ACTION_EDIT, Uri.parse ("content://contacts/people/1"));

intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:1234"));

startActivity (intent);

// 전화번호부

 

Intent intent = new Intent (Intent.ACTION_VIEW, Uri.parse ("geo:0,0"));

startActivity (intent);

// Google Map 이동

 

Intent intent = new Intent (Intent.ACTION_WEB_SEARCH, Uri.parse ("http://google.co.kr"));

startActivity (intent);

// 웹페이지 이동

 

System.out.println(xpp.getText());

Log.d("xml","Text "+xpp.getText());

// 안드로이드 로그

 

안드로이드 visible

public void setVisibility(int id, boolean visible) {

ImageView iv;

iv = (ImageView)findViewById(id);

if(visible)

iv.setVisibility(View.VISIBLE);

else

iv.setVisibility(View.INVISIBLE);

}

 

<ImageView android:id="@+id/NonstopPattern1"

android:layout_marginLeft="39dp"

android:layout_marginTop="108dp"

android:layout_width="54dp"

android:layout_height="25dp"

android:src="@drawable/popup_track_org"

/>

src를 지정한 경우

iv = (ImageView)findViewById(R.id.NonstopPattern1);

iv.setImageResource(R.drawable.popup_track_pro);

로 바꿔도 visible을 껐다 켤때 초기화 되어버린다.

 

Android 숫자 자릿수 별로 나누는 방법

GameResultData.score = 1234567;

int count;

 

TextView tv = (TextView)findViewById(R.id.ResultScore0);

count = GameResultData.score%10;

tv.setText(Integer.toString(count));

 

tv = (TextView)findViewById(R.id.ResultScore1);

count = GameResultData.score%100;

count = count/10;

tv.setText(Integer.toString(count));

 

tv = (TextView)findViewById(R.id.ResultScore2);

count = GameResultData.score%1000;

count = count/100;

tv.setText(Integer.toString(count));

 

tv = (TextView)findViewById(R.id.ResultScore3);

count = GameResultData.score%10000;

count = count/1000;

tv.setText(Integer.toString(count));

 

tv = (TextView)findViewById(R.id.ResultScore4);

count = GameResultData.score%100000;

count = count/10000;

tv.setText(Integer.toString(count));

 

tv = (TextView)findViewById(R.id.ResultScore5);

count = GameResultData.score%1000000;

count = count/100000;

tv.setText(Integer.toString(count));

 

tv = (TextView)findViewById(R.id.ResultScore6);

count = GameResultData.score%10000000;

count = count/1000000;

tv.setText(Integer.toString(count));

// 굳이 이렇게 하지 않아도 String으로 변환후에 charAt으로 해도 된다.

 

String str = Integer.toString(GameResultData.score);

tv.setText(Character.toString(str.charAt(0)));

 

 

안드로이드 XMLParser

package com.android.XMLParser;

import java.io.InputStream;

import java.io.StringReader;

import java.net.URL;

import java.net.URLConnection;

import java.util.ArrayList;

import java.util.List;

import org.xmlpull.v1.XmlPullParser;

import org.xmlpull.v1.XmlPullParserFactory;

import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.widget.TextView;

 

public class XMLParser extends Activity {

TextView textView;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

textView = (TextView) findViewById(R.id.testText);

List<String> strList = getPosts("medits");

Log.d("xml",strList.toString());

textView.setText(strList.toString());

}

private InputStream getInputStream(String str) {

try {

URL url = new URL(str);

URLConnection con = url.openConnection();

InputStream is = con.getInputStream();

return is;

} catch (Exception e) {

Log.d("url error", e.getMessage());

return null;

}

}

public List<String> getPosts(String screen_name) {

List<String> stringList = new ArrayList<String>();

try {

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();

factory.setNamespaceAware(true);

XmlPullParser xpp = factory.newPullParser();

InputStream stream = getInputStream("http://api.twitter.com/1/users/show.xml?screen_name="+ screen_name);

xpp.setInput(stream, "UTF-8");

//xpp.setInput(new StringReader ("<foo>Hello World!</foo>"));

int eventType = xpp.getEventType();

while (eventType != XmlPullParser.END_DOCUMENT) {

switch (eventType) {

case XmlPullParser.START_DOCUMENT:

Log.d("xml","Start document");

break;

case XmlPullParser.END_DOCUMENT:

Log.d("xml","End document");

break;

case XmlPullParser.START_TAG:

Log.d("xml","Start tag "+xpp.getName());

if (xpp.getName().equals("body")) {

eventType = xpp.next();

stringList.add(xpp.getText());

}

break;

case XmlPullParser.END_TAG:

Log.d("xml","End tag "+xpp.getName());

break;

case XmlPullParser.TEXT:

Log.d("xml","Text "+xpp.getText());

break;

}

eventType = xpp.next();

}

return stringList;

} catch (Exception e) {

Log.d("xml error", e.getMessage());

return null;

}

}

}

// documentBuilder 한글 인식 안됨. XmlPullParserFactory 사용

 

안드로이드 I/O

FileOutputStream fos;

FileInputStream fis;

byte[] b;

 

try {

fos = openFileOutput("scenario.xml", Context.MODE_PRIVATE);

fis = openFileInput("scenario.xml");

try {

//fos.write(str.getBytes());

//fos.close();

fis.read(b);

//str = b.toString();

String str = new String(b);

fis.close();

Log.d("tt", str);

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

 

안드로이드 Preferences

FileInputStream, FileOutputStream으로 해도 되지만 이렇게 설정관련 함수를 제공한다.

데이터 로드

SharedPreferences pref = getSharedPreferences("TapSonic", 0); // 두번째 파라미터는 default 값

pref.getString("Key Sound", "0");

pref.getString("Themes", "0");

pref.getString("Vibration", "0");

데이터 저장

SharedPreferences pref = getSharedPreferences("TapSonic", 0);

SharedPreferences.Editor editor = pref.edit();

editor.putString("Key Sound", "1");

 

Android openFileInput

FileInputStream fis = openFileInput(path);

InputStream fis = new BufferedInputStream(openFileInput(path));

or

FileInputStream fis = new FileInputStream(path);

InputStream fis = new BufferedInputStream(new FileInputStream(path));

// 아래의 것이 더 빠르다고는 하는데 별 차이없는 듯

 

안드로이드 투명 액티비티

AndroidManifest.xml

<activity android:name=".GameOptionPopup"

android:theme="@style/TapSonic.Transparent" android:screenOrientation="landscape"/>

 

colors.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

<color name="transparent_color">#A0000000</color>

</resources>

 

styles.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

<style name="TapSonic" />

<style name="TapSonic.Transparent">

<item name="android:windowNoTitle">true</item>

<item name="android:windowFullscreen">true</item>

<item name="android:windowBackground">@color/transparent_color</item>

<item name="android:windowIsTranslucent">true</item>

<item name="android:windowAnimationStyle">@android:style/Animation</item>

</style>

</resources>

 

안드로이드 웹 bitmap 다운

// HTTP URL로부터 이미지 파일을 다운로드받아 로컬에 저장한다.

static boolean HttpDown(String Url, String FileName) {

URL imageurl;

int Read;

try {

imageurl = new URL(Url);

HttpURLConnection conn= (HttpURLConnection)imageurl.openConnection();

conn.connect();

int len = conn.getContentLength();

byte[] raster = new byte[len];

InputStream is = conn.getInputStream();

FileOutputStream fos = G.MainContext.openFileOutput(FileName, 0);

// G.MainContext는 해당 클래스의 context

for (;;) {

Read = is.read(raster);

if (Read <= 0) {

break;

}

fos.write(raster,0, Read);

}

 

is.close();

fos.close();

} catch (Exception e) {

return false;

}

return true;

}

 

다음 코드로 이미지 뷰에 대입

 

Bitmap bitmap = BitmapFactory.decodeFile(템프 파일 경로);

tw.mImage.setImageBitmap(bitmap);

======================================================

 

public Bitmap getConnImg(int call){

URL imgUrl;

Bitmap bm = null;

try {

imgUrl = new URL(getImgUrl(call));

URLConnection conn = imgUrl.openConnection();

HttpURLConnection httpConn = (HttpURLConnection)conn;

InputStream in = httpConn.getInputStream();

bm = BitmapFactory.decodeStream(in);

in.close();

} catch (MalformedURLException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}catch(IOException e){}

return bm;

}

 

Android WebView

WebView wv = (WebView) findViewById(R.id.wv1);

wv.loadData("<img src='http://www.androidside.com/skin/mw.builder/mw.basic.3/img/mw_logo.gif'>", "text/html","utf-8");

 

안드로이드 버튼 레이아웃

<LinearLayout

android:layout_marginTop="114dp"

android:gravity="center"

android:layout_width="307dp"

android:layout_height="43dp"

>

<ImageButton android:id="@+id/SingleBasicBtn"

android:layout_width="92dp"

android:layout_height="43dp"

android:background="#00000000"

android:src="@drawable/popup_quick_btn_org_nm"

/>

<ImageButton android:id="@+id/SingleProBtn"

android:layout_width="92dp"

android:layout_height="43dp"

android:background="#00000000"

android:src="@drawable/popup_quick_btn_pro_nm"

/>

<ImageButton android:id="@+id/SingleLgdBtn"

android:layout_width="92dp"

android:layout_height="43dp"

android:background="#00000000"

android:src="@drawable/popup_quick_btn_lgd_nm"

/>

</LinearLayout>

// 버튼 2개만 필요할 때

btLgd.setVisibility(View.GONE);

 

 

반응형
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2024/12   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30 31
글 보관함