RemoteViews -- 应用

一、RemoteViews在通知栏上的应用

private void testNotification() {
    Notification notification = new Notification();
    notification.icon = R.mipmap.ic_launcher;
    notification.tickerText = "hello world";
    notification.when = System.currentTimeMillis();
    notification.flags = Notification.FLAG_AUTO_CANCEL;
    Intent intent = new Intent(this, Test1Activity.class);
    PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
    RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.notification_layout);
    remoteViews.setTextViewText(R.id.notification_tv, "hello android");
    notification.contentView = remoteViews;
    notification.contentIntent = pendingIntent;
    NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.notify(1, notification);
}

二、RemoteViews在桌面小部件上的应用

//new_app_widget_info.xml




//new_app_widget.xml


    



//NewAppWidget 
public class NewAppWidget extends AppWidgetProvider {
    private static final String TAG = "NewAppWidget";

    static void updateAppWidget(Context context, AppWidgetManager appWidgetManager,
                                int appWidgetId) {
        Log.d("TAG", "zwm, updateAppWidget");
        CharSequence widgetText = context.getString(R.string.appwidget_text);
        // Construct the RemoteViews object
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.new_app_widget);
        views.setTextViewText(R.id.appwidget_text, widgetText);

        // Instruct the widget manager to update the widget
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) { //小部件被添加时或者每次小部件更新时都会调用该方法
        // There may be multiple widgets active, so update all of them
        Log.d("TAG", "zwm, onUpdate");
        for (int appWidgetId : appWidgetIds) {
            updateAppWidget(context, appWidgetManager, appWidgetId);
        }
    }

    @Override
    public void onEnabled(Context context) { //小部件第一次被添加到桌面时调用该方法,可添加多次但只在第一次调用
        // Enter relevant functionality for when the first widget is created
        Log.d("TAG", "zwm, onEnabled");
    }

    @Override
    public void onDisabled(Context context) { //当最后一个该类型的小部件被删除时调用该方法
        // Enter relevant functionality for when the last widget is disabled
        Log.d("TAG", "zwm, onDisabled");
    }
}

//AndroidManifest.xml

    
        
    

    

你可能感兴趣的:(RemoteViews -- 应用)