created travelpostwidget.xml to be used as the widget layout

added required information into travelpostwidgetinfo.xml

TravelPostWidget.java 
created onReceive(), onUpdate(), onDestroy()
added debug logs to TravelPostWidget.java

added TravelPostWidget.java to the AndroidManifest.xml

got the widget button to start the service via a pending intent

basic testing seems to be all working, i need to run some more test on the service befor i call it done
This commit is contained in:
2011-01-24 01:30:15 +00:00
parent 1e39e770bd
commit db27e1528a
6 changed files with 127 additions and 26 deletions

View File

@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.TwentyCodes.android.TravelPost" android:versionCode="0" android:versionName="0.0.0">
package="com.TwentyCodes.android.TravelPost" android:versionCode="0"
android:versionName="0.0.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
@@ -10,11 +11,10 @@
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</activity>
<service android:name="LocationService" android:enabled="true"
android:process=":locationservice" android:exported="true">
</service>
<receiver android:name="LocationReceiver" android:exported="true"
android:enabled="true" android:process=":locationreceiver">
@@ -23,7 +23,15 @@
</intent-filter>
</receiver>
</application>
<receiver android:label="@string/app_name" android:name="TravelPostWidget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider" android:resource="@xml/travelpostwidgetinfo"/>
</receiver>
<service android:process=":TravelPostLocationService" android:enabled="true" android:name="LocationService" android:exported="true"></service>
</application>
<uses-sdk android:minSdkVersion="7" />
@@ -33,4 +41,5 @@
<uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_MOCK_LOCATION"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>
</manifest>

View File

@@ -13,8 +13,11 @@ public final class R {
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int widgetbutton=0x7f060000;
}
public static final class layout {
public static final int main=0x7f030000;
public static final int travelpostwidget=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f050001;

View File

@@ -4,9 +4,10 @@
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/widgetbutton"
android:text="Post"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>

View File

@@ -1,4 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider
xmlns:android="http://schemas.android.com/apk/res/android">
</appwidget-provider>
xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="146dip"
android:minHeight="72dip"
android:updatePeriodMillis="10000"
android:initialLayout="@layout/travelpostwidget"/>

View File

@@ -28,12 +28,13 @@ public class LocationService extends Service implements LocationListener {
/**
* The desired accuracy in meters
*/
private static final float DESIRED_ACCURACY = 6.0f;
private static final float DESIRED_ACCURACY = 12.0f;
/**
* Maximum running time in milliseconds
*/
private final long MAX_RUN_TIME = 180000L;
public static final String TAG = "LocationService";
private LocationManager mLocationManager;
private WakeLock mWakeLock;
@@ -57,14 +58,8 @@ public class LocationService extends Service implements LocationListener {
* @return a runnable that will start the service
* @author ricky barrette
*/
public static Runnable startService(final Context context){
return new Runnable(){
@Override
public void run(){
Intent service = new Intent(context, LocationService.class);
context.startService(service);
}
};
public static Intent startService(final Context context){
return new Intent(context, LocationService.class);
}
/**
@@ -88,6 +83,7 @@ public class LocationService extends Service implements LocationListener {
* @author ricky barrette
*/
private void broadcastLocation() {
Log.v(TAG, "broadcastLocation()");
if (mLocation != null) {
Intent locationUpdate = new Intent();
locationUpdate.setAction(LocationReceiver.ACTION_UPDATE);
@@ -118,6 +114,7 @@ public class LocationService extends Service implements LocationListener {
*/
@Override
public void onCreate(){
Log.v(TAG, "onCreate()");
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
mWakeLock = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);

View File

@@ -1,13 +1,101 @@
/**
* TravelPostWidget.java
* @date Jan, 15, 2011
* @author warren powers
*/
package com.TwentyCodes.android.TravelPost;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.RemoteViews;
/**
* This class will display the widget on the android screen and handle user interaction
* @author warren
*
* @author ricky barrette
*/
public class TravelPostWidget extends AppWidgetProvider {
/**
* Set to true when the application is being debugged.
*/
private static final boolean DEBUG = true;
public final String TAG = "TravelPostWidget";
/**
* Called in response to the ACTION_APPWIDGET_UPDATE broadcast when this AppWidget provider is being asked to provide RemoteViews for a set of AppWidgets.
* Override this method to implement your own AppWidget functionality.
* @see android.appwidget.AppWidgetProvider#onUpdate(android.content.Context, android.appwidget.AppWidgetManager, int[])
* @param context
* @param appWidgetManager
* @param appWidgetIds
* @author ricky barrette
*/
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
if(DEBUG)
Log.v(TAG, "onUpdate()");
final int N = appWidgetIds.length;
}
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
// Create a pending intent to start the location service;
PendingIntent pendingIntent = PendingIntent.getService(context, 0, LocationService.startService(context), 0);
// Get the layout for the App Widget and attach an on-click listener to the button
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.travelpostwidget);
views.setOnClickPendingIntent(R.id.widgetbutton, pendingIntent);
// Tell the AppWidgetManager to perform an update on the current App Widget
appWidgetManager.updateAppWidget(appWidgetId, views);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);
}
/**
* Implements onReceive(Context, Intent) to dispatch calls to the various other methods on AppWidgetProvider.
* (non-Javadoc)
* @see android.appwidget.AppWidgetProvider#onReceive(android.content.Context, android.content.Intent)
* @param context
* @param intent received
* @author ricky barrette
*/
@Override
public void onReceive(Context context, Intent intent) {
if(DEBUG)
Log.v(TAG, "onReceive");
// v1.5 fix that doesn't call onDelete Action
final String action = intent.getAction();
if (AppWidgetManager.ACTION_APPWIDGET_DELETED.equals(action)) {
final int appWidgetId = intent.getExtras().getInt(AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) {
this.onDeleted(context, new int[] { appWidgetId });
}
} else {
super.onReceive(context, intent);
}
}
/**
* Called in response to the ACTION_APPWIDGET_DELETED broadcast when one or more AppWidget instances have been deleted.
* Override this method to implement your own AppWidget functionality.
* (non-Javadoc)
* @see android.appwidget.AppWidgetProvider#onDeleted(android.content.Context, int[])
* @param context
* @param appWidgetIds
* @author ricky barrette
*/
@Override
public void onDeleted(Context context, int[] appWidgetIds) {
if(DEBUG)
Log.v(TAG, "onDelete()");
super.onDeleted(context, appWidgetIds);
}
}