public class LocUpdator extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private LocationManager service;
private String provider;
/** Called when the activity is first created. */
@Override
public void onStart() {
super.onStart();
//setContentView(R.layout.main);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
service = (LocationManager) getSystemService(LOCATION_SERVICE);
boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER);
// Check if enabled and if not send user to the GSP settings
// Better solution would be to display a dialog and suggesting to
// go to the settings
if (!enabled) {
Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(intent);
}
Criteria criteria = new Criteria();
//criteria.setAccuracy(Criteria.ACCURACY_FINE);
//criteria.setPowerRequirement(Criteria.POWER_MEDIUM);
provider = locationManager.getBestProvider(criteria,true);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
Toast.makeText(this, "Provider " + provider + " has been selected." ,Toast.LENGTH_SHORT).show();
onLocationChanged(location);
} else {
latituteField.setText("Is GPS ON ?");
longitudeField.setText("Sorry! No GPS");
}
}
/* Request updates at startup */
@Override
protected void onResume() {
super.onResume();
locationManager.requestLocationUpdates(provider, 400, 1, this);
}
/* Remove the locationlistener updates when Activity is paused */
@Override
protected void onPause() {
super.onPause();
locationManager.removeUpdates(this);
}
@Override
public void onLocationChanged(Location location) {
double lat = (double) (location.getLatitude());
double lng = (double) (location.getLongitude());
//Http Post method to send data to srver Php document
Toast.makeText(this, "Transfered location: "+lat+" "+lng ,Toast.LENGTH_SHORT).show();
Toast.makeText(this, "Currently using " + provider + " technology." ,Toast.LENGTH_SHORT).show();
//latituteField.setText(String.valueOf(lat));
//longitudeField.setText(String.valueOf(lng));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
Toast.makeText(this, "Location Change Notification !!" ,Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderEnabled(String provider) {
Toast.makeText(this, "Enabled new provider " + provider,
Toast.LENGTH_SHORT).show();
}
@Override
public void onProviderDisabled(String provider) {
Toast.makeText(this, "Disabled provider " + provider,
Toast.LENGTH_SHORT).show();
}
}
私のこの活動を一定の時間間隔で開始したいのですが、Alarm ManagerとBroadcastRecieverを試しましたが、...放送受信機からこれを開始する方法が見つかりません...
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//Toast.makeText(context, "Location Cordinates Updating!!!!.",Toast.LENGTH_LONG).show();
Toast.makeText(context, "Current Location :",Toast.LENGTH_LONG).show();
Intent startIntent = new Intent(context, LocUpdator.class);
context.startActivity(startIntent);
startIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
上はBroadcastRecieverです
AlarmActivityは以下のとおりです
public class AlarmActivity extends Activity{/**アクティビティが最初に作成されたときに呼び出されます。* /
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void startAlert(View view) {
//EditText text = (EditText) findViewById(R.id.time);
int i = 20;//Integer.parseInt(text.getText().toString());
//int i= 10;
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
Intent intent = new Intent(this, MyBroadcastReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
this.getApplicationContext(), 234324243, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis()+ (i * 1000), pendingIntent);
//long timerInterval= i*1000;
//alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), timerInterval, pendingIntent);
//finish();
Toast.makeText(this, "DB Update Interval set as " + i + " seconds",
Toast.LENGTH_LONG).show();
}
}
ここで私を助けてください:-)
ログCatエラーは以下のとおりです
09-10 09:50:15.368: E/AndroidRuntime(671): FATAL EXCEPTION: main
09-10 09:50:15.368: E/AndroidRuntime(671): java.lang.RuntimeException: Unable to start activity ComponentInfo{twin.geolocator/twin.geolocator.insideActivity}: java.lang.IllegalArgumentException: provider==null
09-10 09:50:15.368: E/AndroidRuntime(671): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
09-10 09:50:15.368: E/AndroidRuntime(671): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-10 09:50:15.368: E/AndroidRuntime(671): at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-10 09:50:15.368: E/AndroidRuntime(671): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-10 09:50:15.368: E/AndroidRuntime(671): at android.os.Handler.dispatchMessage(Handler.java:99)
09-10 09:50:15.368: E/AndroidRuntime(671): at android.os.Looper.loop(Looper.java:137)
09-10 09:50:15.368: E/AndroidRuntime(671): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-10 09:50:15.368: E/AndroidRuntime(671): at java.lang.reflect.Method.invokeNative(Native Method)
09-10 09:50:15.368: E/AndroidRuntime(671): at java.lang.reflect.Method.invoke(Method.java:511)
09-10 09:50:15.368: E/AndroidRuntime(671): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-10 09:50:15.368: E/AndroidRuntime(671): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-10 09:50:15.368: E/AndroidRuntime(671): at dalvik.system.NativeStart.main(Native Method)
09-10 09:50:15.368: E/AndroidRuntime(671): Caused by: java.lang.IllegalArgumentException: provider==null
09-10 09:50:15.368: E/AndroidRuntime(671): at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1153)
09-10 09:50:15.368: E/AndroidRuntime(671): at twin.geolocator.insideActivity.onCreate(insideActivity.java:32)
09-10 09:50:15.368: E/AndroidRuntime(671): at android.app.Activity.performCreate(Activity.java:5008)
09-10 09:50:15.368: E/AndroidRuntime(671): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-10 09:50:15.368: E/AndroidRuntime(671): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
09-10 09:50:15.368: E/AndroidRuntime(671): ... 11 more
public class insideActivity extends Activity implements LocationListener{
private LocationManager locationManager;
private String provider;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.main);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the locatioin provider -> use
// default
Criteria c = new Criteria();
c.setAccuracy(Criteria.ACCURACY_FINE);
c.setAltitudeRequired(false);
c.setBearingRequired(false);
c.setSpeedRequired(false);
c.setCostAllowed(true);
c.setPowerRequirement(Criteria.POWER_HIGH);
provider = locationManager.getBestProvider(c,true);
Location location = locationManager.getLastKnownLocation(provider);
// Initialize the location fields
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
onLocationChanged(location);
} else {
Log.d("Location not available", "inside");
Log.d("Location not available", "inside ");
}
Log.d("Inside onCreate", "1");
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
Log.d("Inside onLocationChanged", "2");
}
@Override
public void onProviderDisabled(String arg2) {
// TODO Auto-generated method stub
Log.d("Inside onProviderDisabled", "3");
}
@Override
public void onProviderEnabled(String arg3) {
// TODO Auto-generated method stub
Log.d("Inside onProviderEnabled", "4");
}
@Override
public void onStatusChanged(String arg1, int status, Bundle extras) {
// TODO Auto-generated method stub
Log.d("onStatusChanged", "");
}
}