将数据发送到我(主屏幕)小部件到我、部件、屏幕、数据

2023-09-08 08:56:08 作者:梦如此暧昧

我有从我的活动有一定的麻烦发送数据(串),以我的appWidgetProvide类。

我有一个名为updateWidget方法。这个被称为小部件配置活动时,小部件首次被放置在主屏幕上。此法也被称为用的onReceive方法时,它从我的活动,当用户将数据输入到该活动的一个数据。

下面是我的问题:小部件的放入主屏幕,在正确的地方的所有数据。但是,当我的活动发送的数据的onReceive(使用意图和群众演员)数据不来through.I刚上车的extras.getString一个空字符串。

我已经使用意图之前活动之间发送数据,我需要做不同的事情时,我将数据发送到一个小窗口提供类?还是我只是愚蠢和缺少明显的东西?

我已经连接(我认为是)的code相关位。让我知道如果你需要任何澄清或更多的code。

感谢您抽出宝贵时间来阅读这一点,任何帮助,你可以给,

干杯Rakshak

在onListItemClick在小部件配置类。

  @覆盖
    保护无效onListItemClick(ListView的L,视图V,INT位置,长的id){
        super.onListItemClick(L,V,位置ID);

        光标注= mDbHelper.fetchNote(ID);
        startManagingCursor(注);
        字符串标题= note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE));
        字符串文本= note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY));
        loadData(职称);


        意图resultValue =新意图();
        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID,mAppWidgetId);
        的setResult(RESULT_OK,resultValue);
        完();
 }



 无效loadData(字符串名称){

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(本);

    SingleNote.updateWidget(这一点,appWidgetManager,mAppWidgetId,标题);

}
 

这将数据发送到的onReceive的目的(这是在我的活动的一个类)

 私人无效updateWidget(){
    意图I =新的意图(这一点,SingleNote.class);
    i.setAction(SingleNote.UPDATE_ACTION);
    Toast.makeText(getApplicationContext(),mTitleText.getText()+的活动,
            Toast.LENGTH_SHORT).show(); //这只是正常
    i.putExtra(标题,mTitleText.getText());
    sendBroadcast(ⅰ);
 
iOS 14新功能 iPhone主屏幕将支持小部件,无需下载APP即可使用

}

我的窗口小部件提供一流

 公共类SingleNote扩展AppWidgetProvider {

公共静态字符串UPDATE_ACTION =ActionUpdateSinglenoteWidget;
私有静态NotesDbAdapter mDbHelper;



公共无效的OnUpdate(上下文的背景下,AppWidgetManager appWidgetManager,INT [] appWidgetIds){
    最终诠释N = appWidgetIds.length;

    //执行属于此提供这一循环过程中的每个应用程序的widget
    的for(int i = 0; I&n种;我++){
        INT appWidgetId = appWidgetIds [I]



     //告诉AppWidgetManager对当前应用程序部件进行更新
        RemoteViews意见=新RemoteViews(context.getPackageName(),R.layout.singlenote_widget);
        appWidgetManager.updateAppWidget(appWidgetId,意见);




    }
}

@覆盖
公共无效的onReceive(上下文的背景下,意图意图){

        串动= intent.getAction();
        捆绑额外= intent.getExtras();
        字符串TITLE1 = extras.getString(标题); //这个值没有来过
        Toast.makeText(背景下,TITLE1,Toast.LENGTH_LONG).show(); //这给出了一个空的空间

        如果(行动= NULL和放大器;!&安培; action.equals(UPDATE_ACTION)){
                最后AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(上下文);
                组件名名称=新的组件名(背景下,SingleNote.class);
                INT [] appWidgetId = AppWidgetManager.getInstance(上下文).getAppWidgetIds(名称);
                最终诠释N = appWidgetId.length;
                如果(N小于1)
                {
                    返回 ;
                }
                其他 {
                    INT的id = appWidgetId [N-1];
                    updateWidget(背景下,appWidgetManager,ID,TITLE1);
                }
        }

        其他 {
                super.onReceive(背景下,意图);
        }
}



静态无效updateWidget(上下文的背景下,AppWidgetManager appWidgetManager,诠释appWidgetId,串题){

    RemoteViews意见=新RemoteViews(context.getPackageName(),R.layout.singlenote_widget);
    views.setTextViewText(R.id.single_note_title,职称);
    appWidgetManager.updateAppWidget(appWidgetId,意见);



}
 

解决方案

我建议尝试更换 i.putExtra(标题,mTitleText.getText()); updateWidget() i.putExtra(标题,mTitleText.getText()的toString());

 字符串TITLE1 = extras.getString(标题);
 

预计字符串,而 mTitleText.getText()返回编辑 - 这可能是一个不匹配

I am having some trouble sending data(strings) from my activity to my appWidgetProvide class.

I have a method called updateWidget. This gets called by the widget configure activity when the widget first gets placed in the home screen .This method is also called by the onReceive method when it receives data from one of my activities when the user enters data into that activity.

Here is my problem : the widget get placed in the home screen with all the data in the right place. But when my activity sends data the onReceive (using intents and extras) the data does not come through.I just get a empty string on the extras.getString .

I have used intents to send data between activities before , do I need to do something different when I send data to a widget provide class? Or am I just being stupid and missing something obvious ?

I have attached (what I think are the) relevant bits of code. Let me know if you need any clarification or any more of the code.

Thanks for taking the time to read this and for any help that you can give,

Cheers Rakshak

the onListItemClick in the widget configure class.

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        Cursor note = mDbHelper.fetchNote(id);
        startManagingCursor(note);
        String title = note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_TITLE));
        String text = note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_BODY));
        loadData(title);


        Intent resultValue = new Intent();
        resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mAppWidgetId);            
        setResult(RESULT_OK,resultValue);   
        finish();
 }



 void loadData(String title) {

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this); 

    SingleNote.updateWidget(this, appWidgetManager, mAppWidgetId, title);

}

The intent that sends data to the onReceive (this is in one of my activity classes)

private void updateWidget() { 
    Intent i = new Intent(this, SingleNote.class); 
    i.setAction(SingleNote.UPDATE_ACTION); 
    Toast.makeText(getApplicationContext(),mTitleText.getText()+"from the activity",
            Toast.LENGTH_SHORT).show();//This works just fine
    i.putExtra("title", mTitleText.getText());
    sendBroadcast(i); 

}

My widget provide class

public class SingleNote extends AppWidgetProvider {

public static String UPDATE_ACTION = "ActionUpdateSinglenoteWidget";
private static NotesDbAdapter mDbHelper;



public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
    final int N = appWidgetIds.length;

    // Perform this loop procedure for each App Widget that belongs to this provider
    for (int i=0; i<N; i++) {
        int appWidgetId = appWidgetIds[i];



     // Tell the AppWidgetManager to perform an update on the current app widget
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.singlenote_widget);
        appWidgetManager.updateAppWidget(appWidgetId, views);




    }
}

@Override 
public void onReceive(Context context, Intent intent) { 

        String action = intent.getAction(); 
        Bundle extras = intent.getExtras(); 
        String title1 = extras.getString("title");//this value does not come through
        Toast.makeText(context, title1,Toast.LENGTH_LONG).show();//this gives an empty space

        if (action != null && action.equals(UPDATE_ACTION)) { 
                final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 
                ComponentName name = new ComponentName(context, SingleNote.class);
                int[] appWidgetId = AppWidgetManager.getInstance(context).getAppWidgetIds(name);
                final int N = appWidgetId.length;
                if (N < 1)
                {
                    return ;
                }
                else {
                    int id = appWidgetId[N-1];
                    updateWidget(context, appWidgetManager, id ,title1);
                }
        } 

        else { 
                super.onReceive(context, intent); 
        } 
} 



static void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, String title){

    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.singlenote_widget);
    views.setTextViewText(R.id.single_note_title, title);
    appWidgetManager.updateAppWidget(appWidgetId, views);



}

解决方案

I suggest to try replacing i.putExtra("title", mTitleText.getText()); in updateWidget() with i.putExtra("title", mTitleText.getText().toString());

String title1 = extras.getString("title");

expects string, and mTitleText.getText() returns Editable - this is likely a mismatch

 
精彩推荐
图片推荐