Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 기본
- layout
- 클래스
- 이클립스
- Graphic
- HTML
- 메서드
- 어노테이션
- 안드로이드
- struts2
- AWT
- Android
- 에러페이지
- JSP
- Menu
- 전화걸기
- 생성자
- 배열
- Spring
- Java
- paint
- oracle
- OGNL
- Eclips
- 오버로딩
- 메소드
- 국제화
- 예외처리
- mybatis
- JavaScript
Archives
- Today
- Total
note
News Service 주기적으로 데이터 가지고 오기 본문
manifest.xml 서비스 등록 방법
서비스
해당 클래스 클릭후 저장
실행 화면
main.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" > <Button android:id="@+id/newsstart" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="뉴스 서비스 시작" /> <Button android:id="@+id/newsend" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="뉴스 서비스 종료" /> </LinearLayout>
NewsDemo.java
package kr.android.news.service;
//주기적으로 데이터 가지고오기
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class NewsDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnstart = (Button)findViewById(R.id.newsstart);
btnstart.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v) {
Intent intent = new Intent(NewsDemo.this,NewsService.class);
//서비스 실행
startService(intent);
}
});
Button btnend = (Button)findViewById(R.id.newsend);
btnend.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
Intent intent = new Intent(NewsDemo.this, NewsService.class);
//서비스 종료
stopService(intent);
}
});
}
}
NewsService.java
package kr.android.news.service;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;
import android.os.Bundle;
import android.os.Message;
import android.os.Handler;
public class NewsService extends Service{
boolean mQuit;
public void onDestroy(){
super.onDestroy();
Toast.makeText(this, "Service End",4000).show();
//run() for 문 빠져나오기 위함
mQuit = true;
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onStart(Intent intent, int startId){
super.onStart(intent, startId);
NewsThread thread = new NewsThread();
thread.start();
}
class NewsThread extends Thread {
String[] arNews = {
"일본 , 독도는 한국땅으로 인정",
"번데기 맛 쵸코파이 출시",
"춘천 지역에 초거대 유전 발견",
"한국 월드컵 결승 진출",
"국민 소득 6만불 돌파",
"학교 폭력 완전 근절된 것으로 조사 되지 않음",
"안드로이드 점유율 아이폰을 앞 질렀다",
};
public void run(){
for(int idx = 0; mQuit == false; idx++){
Message msg = new Message();
Bundle bundle = new Bundle();
//랜덤으로 나오기 위한
bundle.putString("news",arNews[idx % arNews.length]);
msg.setData(bundle);
mHandler.sendMessage(msg);
try{
Thread.sleep(7000);
}catch (Exception e){
e.printStackTrace();
}
}
}
}
Handler mHandler = new Handler() {
public void handleMessage(Message msg){
String news = msg.getData().getString("news");
Toast.makeText(NewsService.this,news,4000).show();
}
};
}
'Android > 기본' 카테고리의 다른 글
터치해서 화면 전환 Flipper Touch (0) | 2012.01.18 |
---|---|
ListIcon (ListView에 아이콘,버튼) (0) | 2012.01.18 |
SMSReceiver (BroadcastReceiver사용) (0) | 2012.01.18 |
Notify 알림 메세지 (0) | 2012.01.18 |
전화걸기2 컨텍트 프로바이더contact (0) | 2012.01.17 |