開發基于位置的 Android 應用

openkk 12年前發布 | 4K 次閱讀 1.8版本發布 Windows

[代碼] MovieSearchWidget.java

package com.javacodegeeks.android.apps.moviesearchapp.widget;

import android.app.PendingIntent; import android.app.Service; import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetProvider; import android.content.ComponentName; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.IBinder; import android.widget.RemoteViews;

import com.javacodegeeks.android.apps.moviesearchapp.R; import com.javacodegeeks.android.apps.moviesearchapp.model.Movie; import com.javacodegeeks.android.apps.moviesearchapp.services.MovieSeeker;

public class MovieSearchWidget extends AppWidgetProvider {

private static final String IMDB_BASE_URL = "

@Override public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { // To prevent any ANR timeouts, we perform the update in a service context.startService(new Intent(context, UpdateService.class)); }

public static class UpdateService extends Service {

   private MovieSeeker movieSeeker = new MovieSeeker();

    @Override
    public void onStart(Intent intent, int startId) {
        // Build the widget update for today
        RemoteViews updateViews = buildUpdate(this);

        // Push update for this widget to the home screen
        ComponentName thisWidget = new ComponentName(this, MovieSearchWidget.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(this);
        manager.updateAppWidget(thisWidget, updateViews);
    }

  public RemoteViews buildUpdate(Context context) {

     Movie movie = movieSeeker.findLatest();               

     String imdbUrl = IMDB_BASE_URL + movie.imdbId;

     // Build an update that holds the updated widget contents
     RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);

     updateViews.setTextViewText(R.id.app_name, getString(R.string.app_name));
     updateViews.setTextViewText(R.id.movie_name, movie.name);

     Intent intent = new Intent();
     intent.setAction("android.intent.action.VIEW");
     intent.addCategory("android.intent.category.BROWSABLE");
     intent.setData(Uri.parse(imdbUrl));

     PendingIntent pendingIntent = 
        PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

     updateViews.setOnClickPendingIntent(R.id.movie_name, pendingIntent);

     return updateViews;

  }

    @Override
    public IBinder onBind(Intent intent) {
        // We don't need to bind to this service
        return null;
    }

}

}</pre>


[代碼] movie_search_widget.xml

<?xml version="1.0" encoding="utf-8"?>

<appwidget-provider xmlns:android="

[代碼] /res/layout/widget_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<appwidget-provider xmlns:android="

[代碼] /res/layout/widget_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="

<TextView
    android:id="@+id/app_name"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_marginTop="14dip"
    android:layout_marginBottom="1dip"
    android:includeFontPadding="false"
    android:singleLine="true"
    android:ellipsize="end"
    style="@style/Text.WordTitle" />

<TextView  
    android:id="@+id/movie_name"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:layout_below="@id/app_name"
    android:paddingRight="5dip"
    android:paddingBottom="4dip"
    android:includeFontPadding="false"
    android:lineSpacingMultiplier="0.9"
    android:maxLines="4"
    android:fadingEdge="vertical"
    style="@style/Text.Movie" />

</RelativeLayout></pre> </div>


[代碼] res/values/styles.xml

<?xml version="1.0" encoding="utf-8"?>

<resources>

<style name="WidgetBackground">
    <item name="android:background">@drawable/widget_bg</item>
</style>


<style name="Text">
</style>

<style name="Text.Title">
    <item name="android:textSize">16sp</item>
    <item name="android:textStyle">bold</item>

    <item name="android:textColor">@android:color/black</item>
</style>

<style name="Text.Movie">
    <item name="android:textSize">13sp</item>
    <item name="android:textColor">@android:color/black</item>

</style>

</resources></pre> </div>


[代碼] AndroidManifest.xml

<application android:icon="@drawable/icon" android:label="@string/app_name">

...
<!-- Broadcast Receiver that will process AppWidget updates --> <receiver android:name=".widget.MovieSearchWidget" android:label="@string/widget_name"> <intent-filter> <action android:name="android.appwidget.action.APPWIDGET_UPDATE" /> </intent-filter> <meta-data android:name="android.appwidget.provider" android:resource="@xml/movie_search_widget" />

    </receiver>

    <!-- Service to perform web API queries -->        
    <service android:name=".widget.MovieSearchWidget$UpdateService" />        

... </application></pre></div>

 本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!