게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
[android] 스레드 사용에서 무반응입니다.
게시물ID : programmer_14217짧은주소 복사하기
작성자 : 세레니드
추천 : 0
조회수 : 717회
댓글수 : 2개
등록시간 : 2015/11/02 02:17:16
옵션
  • 본인삭제금지
어디가 잘못됬는지를 알질 못하겠네요..
 
어디서 문제가 생기는지 찾는 방법을 알려주시면 감사하겠습니다..
 
문제점 : toggle 버튼 클릭시 무반응 ( 앱자체 실행은 잘됩니다.)
 
프로그램 구조 : MainActivity -> WeatherService ->WeatherThread -> Handler -> WeatherAppWidgetProvider
 
캡처.PNG
public class MainActivity extends AppCompatActivity {

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

public void onClick (View v){
ToggleButton tb = (ToggleButton)v;
if(tb.isChecked() == false){
Intent intent = new Intent( this, WeatherService.class);
stopService(intent);
}
else{
Intent intent = new Intent (this, WeatherService.class);
startService(intent);
}
}
 
 

public class WeatherService extends Service {
private Thread thread;
private Handler handler;

public void onCreate() {
super.onCreate();
handler = new WeatherForecastHandler(this);
thread = new WeatherThread(handler);
}

public int onStartCommand(Intent intent, int flags, int startId) {
if (thread.isAlive() == false) {
thread.start();
}
return super.onStartCommand(intent, flags, startId);
}
public void onDestroy() {
super.onDestroy();
thread.interrupt();
}

@Override
public IBinder onBind(Intent intent) {
return null;
}
}
 
 
public class WeatherThread extends Thread{
private Handler handler;
public WeatherThread(Handler h) { handler =h;}

public void run() {
try {
while (true) {
URL url = new URL(
"http://www.kma.go.kr/XML/weather/sfc_web_map.xml");

XmlPullParserFactory parserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = parserFactory.newPullParser();
parser.setInput(url.openStream(), "utf-8");
int parserEvent = parser.getEventType();
String desc = "";
String ta = "";
String region = "";
String name = "";
while (parserEvent != XmlPullParser.END_DOCUMENT) {
switch (parserEvent) {
case XmlPullParser.START_DOCUMENT:
break;
case XmlPullParser.START_TAG:
name = parser.getName();
if (name != null && name.equals("local")) {
desc = parser.getAttributeValue(null, "desc");
ta = parser.getAttributeValue(null, "ta");
}
break;
case XmlPullParser.TEXT:
if (name != null && name.equals("local")) {
region = parser.getText();
}
break;
case XmlPullParser.END_TAG:
if (name != null && name.equals("local")) {
Message msg = handler.obtainMessage();
WeatherForecast wf = new WeatherForecast();
wf.desc = desc;
wf.region = region;
wf.ta = ta;
msg.obj = wf;
handler.sendMessage(msg);
}
name = "";
break;
}
Thread.sleep(1000);
parserEvent = parser.next();
}
}
} catch (InterruptedException e) {
} catch (Exception e) {
e.printStackTrace();
}
}
}
 

 
public class WeatherForecastHandler extends Handler{
private Context context;
public WeatherForecastHandler (Context ctx){
context = ctx;
}
public void handleMessage(Message msg){
super.handleMessage(msg);
WeatherForecast wf = (WeatherForecast)msg.obj;

Intent intent = new Intent();
intent.putExtra("region", wf.region);
intent.putExtra("desc", wf.desc);
intent.putExtra("ta", wf.ta);
intent.setAction("android.intent.action.WEATHER_FORECAST_RECEIVED");
context.sendBroadcast(intent);
}
}
 
 
public class WeatherAppWidgetProvider extends AppWidgetProvider{
public void onReceive (Context context, Intent intent){
String region = intent.getStringExtra("region");
String desc = intent.getStringExtra("desc");
String ta = intent.getStringExtra("ta");

if(region == null) region = "unknown";

AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
RemoteViews remote = new RemoteViews(context.getPackageName(),R.layout.weatherview);
remote.setTextViewText(R.id.textView1, region);
remote.setTextViewText(R.id.textView2, desc);
remote.setTextViewText(R.id.textView3, ta);

appWidgetManager.updateAppWidget(
new ComponentName("com.example.weatherappwidgettest",
"com.example.weatherappwidgettest.WeatherAppWidgetProvider"),
remote);
super.onReceive(context, intent);
}
}
--------------------------------------------------------------------------------------------------
 
public class WeatherForecast {
public String region;
public String desc;
public String ta;
}
 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lkb.weatherappwidgettest" >
<uses-permission android:name="android.permission.INTERNET"/>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<receiver android:name = "WeatherAppWidgetProvider">
<intent-filter>
<action android:name=
"android.appwidget.action.APPWIDGET_UPDATE"/>
<action android:name=
"android.intent.action.WEATHER_FORECAST_RECEIVED"/>
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/weathermeta"/>
</receiver>
</PreferenceScreen>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호