簡體   English   中英

如何從appwidgetprovider收到此意圖包? 內部例子

[英]How do I receive this intent bundle from an appwidgetprovider? Example inside

這是我的RemoteViewsService中的getViewAt。 我需要獲取單擊此處的行的位置,並使用它發送包含在字符串數組中的特定URL。 這樣做的原因是,當單擊appwidget上的listview時,我可以啟動該URL(簡單的書簽小部件)。

public RemoteViews getViewAt(int position){

    RemoteViews row = new RemoteViews(ctxt.getPackageName(),R.layout.widget_list_item);
    row.setTextViewText(R.id.widgetlisttextview,NicknamesW.get(position));
    Intent i = new Intent();
    Bundle extras = new Bundle();

    extras.putString("getrow",URLSW.get(position));
    i.putExtras(extras);

    row.setOnClickFillInIntent(R.id.widgetlisttextview,i);

不知道我是否在這里缺少什么,但這就是我到目前為止所擁有的。

然后這是我的appwidgetprovider,我在這里嘗試完成onclickIntent並啟動URL。

public void onUpdate(Context ctxt, AppWidgetManager appWidgetManager, int[] appWidgetIds) {




    for (int i = 0; i < appWidgetIds.length; i++){
        String teststr = "http://www.dummy.ca";

        Intent svcIntent = new Intent(ctxt,ListViewService.class);
        svcIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,appWidgetIds[i]);
        //
        svcIntent.setData(Uri.parse(svcIntent.toUri(Intent.URI_INTENT_SCHEME)));
        //
        RemoteViews widget = new RemoteViews(ctxt.getPackageName(),R.layout.widget);
        widget.setRemoteAdapter(R.id.WidgetListView,svcIntent);

        // set url
        Intent openurl = new Intent(Intent.ACTION_VIEW, Uri.parse(teststr));

        PendingIntent clickPI = PendingIntent.getActivity(ctxt,0,openurl,PendingIntent.FLAG_UPDATE_CURRENT);


        widget.setPendingIntentTemplate(R.id.WidgetListView,clickPI);


        appWidgetManager.updateAppWidget(appWidgetIds[i],widget);

現在,它只是從字符串teststr啟動虛擬URL。

任何幫助將不勝感激,我可能會采取錯誤的方法,或者只是無法正確接收意圖,我不確定,但我不確定下一步該怎么做。 我不知道捆綁的意圖在哪里結束,但是onclick正在使用虛擬網址。

提前致謝。

編輯1:

這是我到目前為止所做的。

//將動作添加到AppWidgetProvider接收器中的androidmanifest“ com.halls.jon.LaunchURL”和默認的Intent類別中。 只是試圖學習如何做到這一點。

<receiver android:name=".LinksWidget">
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/>
            <action android:name="com.halls.jon.LaunchURL"/>
        <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
        <meta-data android:name="android.appwidget.provider"
            android:resource="@xml/my_widget_provider"/>
    </receiver>

//然后我轉到AppWidgetProvider並更改了一些意圖和未決意圖。 這是代碼:

Intent launch = new Intent("com.halls.jon.LaunchURL");
PendingIntent clickPI = PendingIntent.getBroadcast(ctxt,0,launch,PendingIntent.FLAG_UPDATE_CURRENT);
widget.setPendingIntentTemplate(R.id.WidgetListView,clickPI);

//這是我在AppWidgetProvider中為onReceive編寫的代碼。

Bundle getdata = intent.getExtras();
    if (getdata != null) {
        String URLLaunch = getdata.getString("url");
        Intent launch = new Intent(Intent.ACTION_VIEW, Uri.parse(URLLaunch));
        context.startActivity(launch);
    }
    super.onReceive(context, intent);

嘗試刪除小部件時,我的應用程序崩潰了。 不知道為什么。 任何進一步的幫助,將不勝感激。

您可以使用自定義操作使待處理的點擊Intent轉到清單中的BroadcastReceiver (甚至可能是AppWidgetProvider )。 當您收到此操作時,請檢查其他功能並構建實際的url,然后調用context.startActivity()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM