开发者

Application widget with bundle?

Hey guys, I have application widget, and I want to send some data to the intent that is attached to PendingIntent, by clicking the widget. here's my code

final int N = appWidgetIds.length;
for (int i = 0; i < N; i++) {
    int appWidgetId = appWidgetIds[i];
    Intent intent = new Intent(context, UpComingBDay开发者_JAVA百科s.class);
    if(bdaysAmount != 0){
    Bundle bundle = new Bundle();
    bundle.putIntegerArrayList("WIDGETIDS", tempAllIDS);
    intent.putExtras(bundle);
    System.out.println("bund insertedddddddddddddd.....................");
    }
    PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
        intent, 0);
    RemoteViews remoteView = new RemoteViews(context.getPackageName(),
        R.layout.widget_layout);
    remoteView.setTextViewText(R.id.widget_text, finalText4TextView);
    remoteView.setOnClickPendingIntent(R.id.WidgetImageButton, pendingIntent);
    appWidgetManager.updateAppWidget(appWidgetId, remoteView);
}
super.onUpdate(context, appWidgetManager, appWidgetIds);

I realize always "bund insertedddddddddd......" is printed on CatLog, but the intent's bundle is null.

what is incorrect? how can i send data by clicking on widget. plz dont offer to use services as my code does not have anything with it. many thanks.


Try using FLAG_UPDATE_CURRENT when you create your PendingIntent.


Sorry for answering post after half a year :) For those, who will find this post when get this problem: I have found, that FLAG_UPDATE_CURRENT is not enough:

http://developer.android.com/reference/android/app/PendingIntent.html

This flag will not work if you need more then 1 widget with different data in intents bundles, because Intent will be overrwritten.

To fix this you will need to update intent data:

http://developer.android.com/guide/topics/appwidgets/index.html

// When intents are compared, the extras are ignored, so we need to embed the extras
// into the data so that the extras will not be ignored.
intent.setData(Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME)));


Here is a method in Kotlin to get pending intent to open your activity

fun getPendingIntentMyActivity(context: Context, message: String): PendingIntent {
    val intent = Intent(context, MyActivity::class.java)
    intent.action = APPWIDGET_INTENT
    intent.data = Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))

    val extras = Bundle().apply {
        putString(APPWIDGET_INTENT, APPWIDGET_OPEN_APP)
        putString(APPWIDGET_INTENT_MESSAGE, message)
    }
    intent.putExtras(extras)
    return PendingIntent.getActivity(context, 0, intent, FLAG_UPDATE_CURRENT or FLAG_IMMUTABLE)
}

Then set it in the Widget

remoteViews.setOnClickPendingIntent(R.id.rootView, getPendingIntentMyActivity(context, "Hello World")

Read the Intent in MyActivity:

private fun readIntent() {
    val intentExtras: Bundle? = intent.extras
    intentExtras?.let {
        val intentMessage: String? = intentExtras.getString(APPWIDGET_INTENT_MESSAGE)
        println(intentMessage)
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜