안드로이드 개발 질문/답변
(글 수 45,052)
홈스크린 위젯을 만들면서
GPS와 연동을 시키기 작업을 하고있습니다
홈스크린 소스입니다.
package com.burningroid.gpschange;
import android.app.PendingIntent; import android.app.Service; import android.appwidget.AppWidgetManager; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.location.Criteria; import android.location.Location; import android.location.LocationListener; import android.location.LocationManager; import android.media.AudioManager; import android.os.Bundle; import android.os.IBinder; import android.widget.RemoteViews;
import com.google.android.maps.MapActivity;
public class WidgetService extends Service { private static final String ACTION_BTNCLICK="com.burningroid.gpschange.ACTION_BTNCLICK"; private static int counter = 0;
//Context context; @Override public void onStart(Intent intent, int startId) { // TODO Auto-generated method stub super.onStart(intent, startId); RemoteViews view = new RemoteViews(getPackageName(),R.layout.appwidget); Intent newintent = new Intent(); newintent.setAction(ACTION_BTNCLICK); PendingIntent pending = PendingIntent.getService(this,0,newintent,0); view.setOnClickPendingIntent(R.id.imageview1, pending); if(ACTION_BTNCLICK.equals(intent.getAction())){ btnClicked(view); } AppWidgetManager manager = AppWidgetManager.getInstance(this); ComponentName widget = new ComponentName("com.burningroid.gpschange","com.burningroid.gpschange.GpsChange"); manager.updateAppWidget(widget,view); }
private void btnClicked(RemoteViews view) { AudioManager aManager = (AudioManager)getSystemService(AUDIO_SERVICE); // TODO Auto-generated method stub int[] ids = { R.drawable.sil1, R.drawable.sil2, R.drawable.sil3 }; counter++; /*Toast toast =Toast.makeText(this,"여기까지 왔음1",Toast.LENGTH_SHORT); toast.show();*/ view.setImageViewResource(R.id.imageview1, ids[counter % 3]); switch (counter %3) { case 0: break;
case 1: break; case 2: AutoChanges(); break; } }
private void AutoChanges() { // TODO Auto-generated method stub @SuppressWarnings("unused") class Gpssence extends MapActivity{ float speed; @Override protected void onCreate(Bundle arg0) { // TODO Auto-generated method stub super.onCreate(arg0); String context = Context.LOCATION_SERVICE; LocationManager locationManager = (LocationManager)getSystemService(context); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.NO_REQUIREMENT); criteria.setSpeedRequired(true); String provider = locationManager.getBestProvider(criteria, true); Location location = locationManager.getLastKnownLocation(provider); //updateWithNewLocation(location); locationManager.requestLocationUpdates(provider, 2000, 10, locationListener); }
private void updateWithNewLocation(Location location) { // TODO Auto-generated method stub speed = location.getSpeed(); if(speed == 0.0){ try { Thread.sleep(120000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } } private final LocationListener locationListener = new LocationListener(){ @Override public void onStatusChanged(String provider, int status, Bundle extras) { // TODO Auto-generated method stub } @Override public void onProviderEnabled(String provider) { // TODO Auto-generated method stub updateWithNewLocation(null); } @Override public void onProviderDisabled(String provider) { // TODO Auto-generated method stub } @Override public void onLocationChanged(Location location) { // TODO Auto-generated method stub updateWithNewLocation(location); } }; @Override protected boolean isRouteDisplayed() { // TODO Auto-generated method stub return false; } } }
@Override public IBinder onBind(Intent arg0) { // TODO Auto-generated method stub return null; } }
머 소스는 이렇습니다;; 이것은 메소드로 넘겨봣는데 작동이 안되고
따로 클레스를 만들어서 인텐트로 작동을 시켜볼려고했는데 중간중간에 띠어노은 토스트를 보면 작동이 안됩니다;;
어찌;; 조언좀 부탁드림니다...ㅠㅠ(2일째 이걸로 고민중이에요;;ㅠㅠ)