메인 액티비티에 서비스에서 주기적으로 변동된 사항을 뿌려주는 어플을 만들고 있습니다.
일단 서비스의 소스구요
package com.android.AutoUpdater;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Timer;
import java.util.TimerTask;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import android.app.Service;
import android.content.Intent;
import android.os.Bundle;
import android.os.IBinder;
public class PushData extends Service {
private TimerTask [] mMyTask;
private Timer [] mMyTimer;
private String mPusher ;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate();
// mPusher =" 텍스트를 가져올 주소 " ;
mMyTask = new TimerTask[1];
mMyTimer = new Timer[1];
mMyTask[0] = new TimerTask() {
// 최초 1초 다음실행까지 100초의 텀을 두고 T(); 와 downloadFile(mPusher); 를 실행함
public void run()
{
T();
downloadFile(mPusher);
}
};
mMyTimer[0] = new Timer();
mMyTimer[0].schedule(mMyTask[0], 1, 100);
}
public void T(){
Intent mI = new Intent();
mI.putExtra("MailA",MailAmount.toString());
}
// MailAmount변수값을 인텐트에 싣습니다.
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
String MailAmount; // 텍스트 다운로드 함수. 파라미터는 String형
// fileUrl 이 들어갑니다.
void downloadFile(String fileUrl)
{
URL myFileUrl = null; // URL 타입의 myFileUrl을 NULL로 초기화 시켜줍니다.
try
{
myFileUrl = new URL(fileUrl); // 파라미터로 넘어온 Url을 myFileUrl에 대입합니다.
}
catch(MalformedURLException e) // 예외처리를 해줍니다.
{
// Todo Auto-generated catch block
e.printStackTrace();
}
try
{
// 실질적인 통신이 이루어지는 부분입니다.
// myFileUrl 로 접속을 시도합니다.
HttpURLConnection conn = (HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(true);
conn.connect();
int length = conn.getContentLength(); // 받아온 컨텐츠의 길이를 length 변수에 저장합니다.
DocumentBuilderFactory mdbf = DocumentBuilderFactory.newInstance();
DocumentBuilder mdb = null;
try {
mdb = mdbf.newDocumentBuilder();
}
catch (ParserConfigurationException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch(IOException e) // 예외처리를 해줍니다.
{
e.printStackTrace();
}
}
}
다음은 메인 액티비티쪽 소스인데
package com.android.AutoUpdater;
import java.util.Timer;
import java.util.TimerTask;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class AutoUpdater extends Activity {
/** Called when the activity is first created. */
TextView T1;
private String N1;
private TimerTask [] mMyTask;
private Timer [] mMyTimer;
// 타임태스크와 타이머를 선언
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
T1.findViewById(R.id.T1);
mMyTask = new TimerTask[1];
mMyTimer = new Timer[1];
// 타임태스크와 타이머에 메모리 할당
mMyTask[0] = new TimerTask() {
// 최초 1초 다음실행까지 100초의 텀을 두고 런메소드 안에 내용 실행
public void run()
{
GetIntent();
}
};
mMyTimer[0] = new Timer();
mMyTimer[0].schedule(mMyTask[0], 1, 100);
}
public void GetIntent()
{
Bundle extras = getIntent().getExtras();
N1 = extras.getString("MailA");
// PushData 인텐트 MailA에 실린 데이터를 N1에 스트링형으로 넣어줍니다.
T1.setText(N1);
// N1을 T1에 텍스트 형으로 넣어줍니다.
}
}
실행과 동시에 자바 널포인트 익셉션 에러가 나버리네요 ㅠ
왜 그런지 모르겠어요 ㅠㅠ 살려주세요



