안녕하세요.
fileIO. 파일 입출력에 관한 예제입니다.
activity_main.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="@+id/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save"/>
<Button
android:id="@+id/load"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="load"/>
<Button
android:id="@+id/loadres"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="loadres"/>
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="delete"/>
<EditText
android:id="@+id/edittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="edittext"/>
</LinearLayout>
MainActivity.java
package com.example.fileio;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit = (EditText)findViewById(R.id.edittext);
findViewById(R.id.save).setOnClickListener(clickListener);
findViewById(R.id.load).setOnClickListener(clickListener);
findViewById(R.id.loadres).setOnClickListener(clickListener);
findViewById(R.id.delete).setOnClickListener(clickListener);
}
Button.OnClickListener clickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()){
case R.id.save:
try{
FileOutputStream fos =
openFileOutput("test.txt", Context.MODE_WORLD_READABLE);
String str = "android file io test";
fos.write(str.getBytes());
fos.close();
edit.setText("file sucess");
}catch(Exception e){
Log.i("save", "file fail");
}
break;
case R.id.load:
try{
FileInputStream fis = openFileInput("test.txt");
byte[] data = new byte[fis.available()];
while(fis.read(data) != -1){} fis.close();
edit.setText(new String(data));
}catch (FileNotFoundException fileNotFountError){
edit.setText("File not found");
}catch (IOException ioe){
ioe.printStackTrace();
}
break;
case R.id.loadres:
try{
InputStream fres = getResources().openRawResource(R.raw.myfile);
InputStreamReader reader = new InputStreamReader(fres, "UTf-8");
char[] data = new char[fres.available()];
while(reader.read(data) != -1) {} fres.close();
edit.setText(new String(data));
} catch(Exception e){
}
break;
case R.id.delete:
if(deleteFile("text.txt")){
edit.setText("delete success");
}
else{
edit.setText("delete fail");
}
break;
default:
break;
}
}
};
}
위에 raw라는 폴더에 myfile.txt 를 만들어서 넣어줍니다.
실행결과는 아래와 같이 됩니다.
sqlite database browser
DBAdapter
간단히 DB를 적용해서 출력할 수 있는 예제입니다.
notification 기능에 대한 구현입니다. 안드로이드에서는 간단한 알리미를 보낼 수 있습니다.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="notifi"/>
<Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cancel"/>
</LinearLayout>
MainActivity.java
package com.example.notification;
import android.os.Bundle;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private NotificationManager mNotificationManager;
private Button mNotifiButton;
private Button mCancelButton;
private final static int MESSAGE_KEY = 12345;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
mNotifiButton = (Button) findViewById(R.id.btn0); mNotifiButton.setOnClickListener(clickHandler);
mCancelButton = (Button) findViewById(R.id.btn1); mCancelButton.setOnClickListener(clickHandler);
}
private void notifi() {
String ticker = "there is notification content message but content is long wheel up";
String title = "there is notification title message";
String text = "there is notification content message";
Intent intent = new Intent(this, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this , 0, intent, 0);
Notification notification = new Notification(android.R.drawable.ic_input_add, ticker, System.currentTimeMillis());
notification.setLatestEventInfo(this, title, text, pendingIntent);
mNotificationManager.notify(MESSAGE_KEY, notification);
Toast.makeText(this, "Notification notify.", Toast.LENGTH_SHORT).show();
}
private void notifiCancel() {
mNotificationManager.cancel(MESSAGE_KEY);
Toast.makeText(this, "Notification cancel.", Toast.LENGTH_SHORT).show();
}
Button.OnClickListener clickHandler = new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn0: notifi();
break;
case R.id.btn1: notifiCancel();
break;
}
}
};
}
실행결과는 다음과 같습니다.
교육 수기 금요일 까지