繁体   English   中英

在带有 3 个按钮的 StackView 的主屏幕小部件上,小部件项目

[英]On a home screen widget with a StackView with 3 buttons the widget item

我有一个带有 StackView 的小部件布局。 在小部件项目中,我有三个按钮。 问题是:如何触发 3 个不同的事件(每个按钮一个)。

widget_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="8dip"
    android:background="@drawable/layout_round_corners"
    android:orientation="vertical"
    android:padding="3dp" >

    <TextView
        android:id="@+id/txtTitle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/gradient_bk_text_view"
        android:gravity="center_horizontal"
        android:padding="5dp"
        android:text="My Contacts"
        android:textColor="@color/Azure"
        android:textSize="@dimen/txt_title_size" />

    <StackView
        android:id="@+id/stack_view"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:loopViews="true" />

    <TextView
        android:id="@+id/empty_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/widget_item_background"
        android:gravity="center"
        android:padding="10dp"
        android:text="@string/empty_view_text"
        android:textColor="#ffffff"
        android:textSize="20sp"
        android:textStyle="bold" />

</LinearLayout>

widget_item.xml

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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/widget_item"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@drawable/widget_item_background"
        android:gravity="center"
        android:padding="10dp"
        android:scaleType="fitXY"
        android:src="@drawable/icon_sem_foto"
        android:textColor="#ffffff"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/txtWidgetNome"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#77000000"
        android:ellipsize="end"
        android:gravity="center_vertical|center_horizontal"
        android:lines="1"
        android:padding="5dp"
        android:text="Nome"
        android:textColor="@android:color/background_light"
        android:textSize="@dimen/txtWidgetSize" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageButton
            android:id="@+id/imgbtn_chamada"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:background="@drawable/round_button_green"
            android:src="@android:drawable/sym_action_call" />

        <ImageButton
            android:id="@+id/imgbtn_mensagem"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/round_button_orange"
            android:src="@android:drawable/stat_notify_chat" />

        <ImageButton
            android:id="@+id/imgbtn_email"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/round_button_red"
            android:src="@android:drawable/sym_action_email"
            android:focusableInTouchMode="false" />

    </LinearLayout>

</LinearLayout>

WidgetService.java 上

public RemoteViews getViewAt(int position) {
    RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget_item);

    byte[] img = mContactos.get(position).getFoto();
    Bitmap theImage = null;
    try {
        theImage = BitmapFactory.decodeByteArray(img, 0, img.length);
        rv.setImageViewBitmap(R.id.widget_item, Utils.getResizedBitmap(theImage, 400, 400));
    } catch (Exception e) {
        Log.i(TAG, "-----------------------> Image Exception: " + e.getMessage());
        rv.setImageViewResource(R.id.widget_item, R.drawable.icon_sem_foto);
    }
    rv.setTextViewText(R.id.txtWidgetNome, mContactos.get(position).getNome());

    Bundle extras = new Bundle();
    extras.putInt(StackWidgetProvider.EXTRA_ITEM_POSITION, position);
    extras.putInt(StackWidgetProvider.EXTRA_ITEM_ID, mContactos.get(position).getId());
    extras.putString(StackWidgetProvider.EXTRA_ITEM_NAME, mContactos.get(position).getNome());
    extras.putString(StackWidgetProvider.EXTRA_ITEM_TELEFONE, mContactos.get(position).getTelefone());
    extras.putString(StackWidgetProvider.EXTRA_ITEM_EMAIL, mContactos.get(position).getEmail());

    Intent fillInItemIntent = new Intent();
    fillInItemIntent.putExtras(extras);
    rv.setOnClickFillInIntent(R.id.widget_item, fillInItemIntent);
    rv.setOnClickFillInIntent(R.id.imgbtn_chamada, fillInItemIntent);
    rv.setOnClickFillInIntent(R.id.imgbtn_mensagem, fillInItemIntent);
    rv.setOnClickFillInIntent(R.id.imgbtn_email, fillInItemIntent);

    return rv;
}

并在WidgetProvider.java 上

@Override
public void onReceive(Context context, Intent intent) {
    Log.i(TAG, "-----------------------------> onReceive");
    Log.i(TAG, "-----------------------------> onReceive Action = " + intent.getAction().toString());

    //AppWidgetManager mgr = AppWidgetManager.getInstance(context);
    switch (intent.getAction()) {
        case ACTION_TOAST:
        case ACTION_CALL:
            //int appWidgetId = intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,
            //                                     AppWidgetManager.INVALID_APPWIDGET_ID);
            //int viewIndex = intent.getIntExtra(EXTRA_ITEM_POSITION, 0);
            //int id = intent.getIntExtra(EXTRA_ITEM_ID, 0);

            Toast.makeText(context, "Call to: " + intent.getStringExtra(EXTRA_ITEM_NAME), Toast.LENGTH_SHORT).show();
            MakeChamada(context, intent.getStringExtra(EXTRA_ITEM_TELEFONE));
            break;
        case ACTION_SMS:
            Toast.makeText(context, "SMS to: " + intent.getStringExtra(EXTRA_ITEM_NAME), Toast.LENGTH_SHORT).show();
            MakeSMS(context, intent.getStringExtra(EXTRA_ITEM_TELEFONE));
            break;
        case ACTION_EMAIL:
            Toast.makeText(context, "Send email to: " + intent.getStringExtra(EXTRA_ITEM_NAME), Toast.LENGTH_SHORT).show();
            MakeEmail(context, intent.getStringExtra(EXTRA_ITEM_EMAIL));
            break;
    }
    super.onReceive(context, intent);
}

@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    Log.i(TAG, "-----------------------------> onUpdate");
    // update each of the widgets with the remote adapter
    for (int i = 0; i < appWidgetIds.length; ++i) {

        //Intent intent = new Intent(context, StackWidgetService.class);
        //intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetIds[i]);

        //intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
        //RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
        //rv.setRemoteAdapter(appWidgetIds[i], R.id.stack_view, intent);

        //rv.setEmptyView(R.id.stack_view, R.id.empty_view);

        int groupId = WidgetConfiguration.loadGroupIdPref(context, appWidgetIds[i]);
        CreatePendingIntents(context, appWidgetManager, appWidgetIds[i], groupId);
    }
    super.onUpdate(context, appWidgetManager, appWidgetIds);
}

public static void CreatePendingIntents(Context context, AppWidgetManager appWidgetManager, int appWidgetId, int groupId) {

    ClassWidgets groupWidget;
    DataBaseHelper db = new DataBaseHelper(context);
    groupWidget = db.getwidget(groupId);
    db.close();

    RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
    remoteViews.setRemoteAdapter(appWidgetId, R.id.stack_view, SetContactList(context, appWidgetId, groupId));

    remoteViews.setEmptyView(R.id.stack_view, R.id.empty_view);

    remoteViews.setTextViewText(R.id.txtTitle, groupWidget.getDescricao());

    remoteViews.setPendingIntentTemplate(R.id.stack_view, SetToastBroadcastToast(context, appWidgetId));
    remoteViews.setPendingIntentTemplate(R.id.imgbtn_mensagem, SetMenssagemBroadCastToast(context, appWidgetId)); //<------

    //RemoteViews remoteViewsIn = new RemoteViews(context.getPackageName(), R.layout.widget_item);
    //remoteViews.setOnClickPendingIntent(R.id.imgbtn_chamada, GetPendingChamadasIntent(context));
    //remoteViews.setOnClickPendingIntent(R.id.imgbtn_mensagem, GetPendingMensagemIntent(context));
    //remoteViews.setOnClickPendingIntent(R.id.imgbtn_email, GetPendingEmailIntent(context));
    //remoteViews.setOnClickPendingIntent(R.id.imgbtn_chamada, SetChamadaBroadCastToast(context, appWidgetId));
    //remoteViews.setOnClickPendingIntent(R.id.imgbtn_mensagem, SetMensagemBroadCastToast(context, appWidgetId));
    //remoteViews.setOnClickPendingIntent(R.id.imgbtn_email, SetEmailBroadCastToast(context, appWidgetId));

    //remoteViews.addView(R.id.stack_view, remoteViewsIn);

    appWidgetManager.updateAppWidget(appWidgetId, remoteViews);
}

private static PendingIntent SetToastBroadcastToast(Context context, int appWidgetId) {
    Intent toastIntent = new Intent(context, StackWidgetProvider.class);
    toastIntent.setAction(StackWidgetProvider.ACTION_TOAST);
    toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    return toastPendingIntent;
}

private static PendingIntent SetMenssagemBroadCastToast(Context context, int appWidgetId) {  // <----
    Intent toastIntent = new Intent(context, StackWidgetProvider.class);
    toastIntent.setAction(StackWidgetProvider.ACTION_SMS);
    toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    return toastPendingIntent;
}

//******************************************************************************************
// Handle Contact List
private static Intent SetContactList(Context context, int appWidgetId, int groupId) {
    Intent intent = new Intent(context, StackWidgetService.class);
    intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    intent.putExtra(GROUP_ID, groupId);
    intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
    return intent;
}

//******************************************************************************************
// Handle Chamadas
private static PendingIntent GetPendingChamadasIntent(Context context) {
    Intent btnChamadasIntent = new Intent(Intent.ACTION_CALL);
    btnChamadasIntent.setData(Uri.parse("tel:" + 964770078));
    PendingIntent pendingIntentChamadas = PendingIntent.getActivity(context, 0, btnChamadasIntent, 0);
    return pendingIntentChamadas;
}

private static PendingIntent SetChamadaBroadCastToast(Context context, int appWidgetId) {
    Intent chamadaIntent = new Intent(context, StackWidgetProvider.class);
    chamadaIntent.setAction(StackWidgetProvider.ACTION_CALL);
    chamadaIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    PendingIntent chamadaPendingIntent = PendingIntent.getBroadcast(context, 0, chamadaIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    return chamadaPendingIntent;
}

private void MakeChamada(Context context, String telefone) {
    Intent btnChamadasIntent = new Intent(Intent.ACTION_CALL);
    btnChamadasIntent.setData(Uri.parse("tel:" + telefone));
    context.startActivity(btnChamadasIntent);
}

//******************************************************************************************
// Handle Mensagens
private static PendingIntent GetPendingMensagemIntent(Context context) {
    Intent btnMensagemIntent = new Intent(Intent.ACTION_SENDTO);
    btnMensagemIntent.addCategory(Intent.CATEGORY_DEFAULT);
    btnMensagemIntent.setType("vnd.android-dir/mms-sms");
    btnMensagemIntent.setData(Uri.parse("sms:" + 964770078));
    btnMensagemIntent.putExtra("sms_body", "Olá!");
    PendingIntent pendingMensagemIntent = PendingIntent.getActivity(context, 0, btnMensagemIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    return pendingMensagemIntent;
}

private static PendingIntent SetMensagemBroadCastToast(Context context, int appWidgetId) {
    Intent mensagemIntent = new Intent(context, StackWidgetProvider.class);
    mensagemIntent.setAction(StackWidgetProvider.ACTION_SMS);
    mensagemIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    PendingIntent mensagemPendingIntent = PendingIntent.getBroadcast(context, 0, mensagemIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    return mensagemPendingIntent;
}

private void MakeSMS(Context context, String telefone) {
    Intent btnMensagemIntent = new Intent(Intent.ACTION_SENDTO);
    btnMensagemIntent.addCategory(Intent.CATEGORY_DEFAULT);
    btnMensagemIntent.setType("vnd.android-dir/mms-sms");
    btnMensagemIntent.setData(Uri.parse("sms:" + telefone));
    btnMensagemIntent.putExtra("sms_body", "Olá!");
    context.startActivity(btnMensagemIntent);
}

//******************************************************************************************
// Handle Email
private static PendingIntent GetPendingEmailIntent(Context context) {
    Intent btnEmailIntent = new Intent(android.content.Intent.ACTION_SEND);
    btnEmailIntent.setType("text/plain");
    btnEmailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, "");
    btnEmailIntent.putExtra(android.content.Intent.EXTRA_CC, "");
    btnEmailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject");
    btnEmailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "emailText");
    PendingIntent pendingEmailIntent = PendingIntent.getActivity(context, 0, btnEmailIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    return pendingEmailIntent;
}

private static PendingIntent SetEmailBroadCastToast(Context context, int appWidgetId) {
    Intent emailIntent = new Intent(context, StackWidgetProvider.class);
    emailIntent.setAction(StackWidgetProvider.ACTION_EMAIL);
    emailIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
    PendingIntent emailPendingIntent = PendingIntent.getBroadcast(context, 0, emailIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    return emailPendingIntent;
}

private void MakeEmail(Context context, String email) {
    Intent btnEmailIntent = new Intent(android.content.Intent.ACTION_SEND);
    btnEmailIntent.setType("text/plain");
    btnEmailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, email);
    btnEmailIntent.putExtra(android.content.Intent.EXTRA_CC, "");
    btnEmailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "subject");
    btnEmailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "emailText");
    context.startActivity(btnEmailIntent);
}

在我的代码中,每次点击只会触发 ACTION_TOAST! 我已经尝试过相同的解决方案(也在代码上)但没有结果! 提前致谢。

所以基本上你必须做两件事才能将 onClick 行为添加到 Android App Widget 中 StackView/ListView 中的按钮。

在实现 RemoteViewService.RemoteServiceFactory 的 StackRemoteViewsFactory 的 getViewAt 函数中,为要添加函数的 id 添加 setOnClickFillingIntent() 。 例如。

 RemoteViews rv = new RemoteViews(mContext.getPackageName(), R.layout.widget__fan);
 Bundle extras = new Bundle();
 extras.putInt(TestWidget.EXTRA_ITEM, position);
 Intent fillInIntent = new Intent();
 fillInIntent.putExtras(extras);
 rv.setOnClickFillInIntent(R.id.fan_status, fillInIntent); // id of button you want to add a onClick function to.

在扩展 AppWidgetProvider 的 WidgetProvider 类的 onUpdate 函数中,添加一个 setPendingIntentTemplate()。 例如。

 Intent toastIntent = new Intent(context, TestWidget.class);
 toastIntent.setAction(TestWidget.act1);
 toastIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
 intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));
 PendingIntent toastPendingIntent = PendingIntent.getBroadcast(context, 0, toastIntent,PendingIntent.FLAG_UPDATE_CURRENT);
 rv.setPendingIntentTemplate(R.id.lview, toastPendingIntent);

现在,已经为该特定按钮设置了操作(在我的情况下为 R.id.fan_status)。 您可以在 onRecieve() 函数中添加其功能。 例如。

public void onReceive(Context context, Intent intent) {    
    if (intent.getAction().equals(act1)) {
       // Add your functionalities
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM