Android 从具有配置活动的小部件启动活动

Android 从具有配置活动的小部件启动活动,android,android-widget,Android,Android Widget,大家好,, 我在从我的主屏幕小部件启动活动时遇到问题。在有人将此标记为重复问题之前,请知道我已经环顾四周并尝试了其他类似问题中提到的解决方案。尤其是这一个 我的问题很简单,我想从我的主屏幕小部件启动一个活动,但它不起作用。 注意:新安装应用程序后,该活动将从小部件启动一次(第一次) 这是小部件提供的代码、配置活动和相关清单条目。如果需要查看更多代码,请告诉我 配置活动: public class SingleNoteConfigure extends ListActivity {

大家好,, 我在从我的主屏幕小部件启动活动时遇到问题。在有人将此标记为重复问题之前,请知道我已经环顾四周并尝试了其他类似问题中提到的解决方案。尤其是这一个

我的问题很简单,我想从我的主屏幕小部件启动一个活动,但它不起作用。 注意:新安装应用程序后,该活动将从小部件启动一次(第一次)

这是小部件提供的代码、配置活动和相关清单条目。如果需要查看更多代码,请告诉我

配置活动:

    public class SingleNoteConfigure extends ListActivity  {


private NotesDbAdapter mDbHelper;
int mAppWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
public static String ACTION_WIDGET_LIST = "ActionReceiverList";
public static String ACTION_WIDGET_NEW = "ActionReceiverNew";



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //set the result to canceled to allow the user
    //to change their mind mid widget configure
    setResult(RESULT_CANCELED);
    Log.d("MYTAG", "in the onCreate of of the widget configure");

    //set the layout file for the widget configure
    setContentView(R.layout.notes_list_config);

    //using the action bar title for user instruction
    setTitle("Select note to display on the widget");

    // Find the widget id from the intent. 
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    if (extras != null) {
        mAppWidgetId = extras.getInt(
                AppWidgetManager.EXTRA_APPWIDGET_ID, AppWidgetManager.INVALID_APPWIDGET_ID);
    }
    Log.d("MYTAG", "in the onCreate");
    //stuff to get the database to open
    mDbHelper = new NotesDbAdapter(this);
    mDbHelper.open();

    //call the method that fills the list view
    fillData();

}

 @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));



        RemoteViews views = new RemoteViews(this.getPackageName(), R.layout.singlenote_widget);
        AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this); 
        appWidgetManager.updateAppWidget(mAppWidgetId, views);    



        Log.d("MYTAG", "in the onListItemClick....");

        loadData(title, text, id);//here id is the row id of the selection;it is returned by the onListItemSelect 

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



 void loadData(String title, String text, Long Id) {

    AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this); 
    Log.d("MYTAG", "in the load data....");
    SingleNote.updateWidget(this, appWidgetManager, mAppWidgetId, title, text);
    NotesDbAdapter.updateWidgetId(mAppWidgetId,Id);

}

private void fillData() {
    Cursor notesCursor = mDbHelper.fetchAllNotes();
    startManagingCursor(notesCursor);
    Log.d("MYTAG", "in the fill data  of the widget configure");
    // Create an array to specify the fields we want to display in the list (TITLE and DATE)
    String[] from = new String[]{NotesDbAdapter.KEY_TITLE,NotesDbAdapter.KEY_DATE};


    // and an array of the fields we want to bind those fields to (title and date)
    int[] to = new int[]{R.id.title,R.id.date};


    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter notes = new SimpleCursorAdapter(this, R.layout.notes_row1, notesCursor, from, to);

    setListAdapter(notes);


}  
}
小部件提供程序:

 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];            

        Log.d("MYTAG", "in the onUpdate");

        Intent intent = new Intent(context, Notepadv3.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.addCategory(Intent.CATEGORY_LAUNCHER);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.singlenote_widget);
        views.setOnClickPendingIntent(R.id.single_note_text, pendingIntent);  

        // Push update for this widget to the home screen

        ComponentName thisWidget = new ComponentName(context, SingleNote.class);
        AppWidgetManager manager = AppWidgetManager.getInstance(context);
        manager.updateAppWidget(thisWidget, views);

    }
}

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

    try
    {

    Log.d("MYTAG", "in the onReceive....");
        String action = intent.getAction(); 
        Log.d("MYTAG", "in the onReceive....line 2");
        Bundle extras = intent.getExtras(); 
        Log.d("MYTAG", "in the onReceive....after bundle");
        String title = extras.getString("title");
        Log.d("MYTAG", "in the onReceive....after title");
        String text = extras.getString("body");
        Log.d("MYTAG", "in the onReceive....after text");
        Log.d("MYTAG", "@ the point of int");
        int id = extras.getInt("widget_id");

        Log.d("MYTAG", action+ title + text + id);

        if (action != "rakshak" && action.equals(UPDATE_ACTION)) { 
            final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); 

            if (id > 0)
            {
                updateWidget(context, appWidgetManager, id ,title ,text);
            }
            else {
                return;

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

    }
    catch (Exception e)
    {
        Log.e("STACKTRACE_TAG", "STACKTRACE");
        Log.e("STACKTRACE_TAG", Log.getStackTraceString(e));
    }


}

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

    Log.d("MYTAG", "in the updatewidget method in the siglenote widget....");
    RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.singlenote_widget);
    views.setTextViewText(R.id.single_note_title, title);
    views.setTextViewText(R.id.single_note_text, text);
    appWidgetManager.updateAppWidget(appWidgetId, views);



}
    }
公共类SingleNote扩展了AppWidgetProvider{
公共静态字符串更新\u ACTION=“ActionUpdateSinglenoteWidget”;
专用静态便笺簿mDbHelper;
公共void onUpdate(上下文上下文,AppWidgetManager AppWidgetManager,int[]AppWidgetId){
final int N=appWidgetIds.length;
//对属于此提供商的每个应用程序小部件执行此循环过程
对于(int i=0;i 0)
{
updateWidget(上下文、appWidgetManager、id、标题、文本);
}
否则{
回来
}
}
否则{
super.onReceive(上下文、意图);
} 
}
捕获(例外e)
{
Log.e(“STACKTRACE_标签”、“STACKTRACE”);
Log.e(“STACKTRACE_标签”,Log.getStackTraceString(e));
}
}
静态void updateWidget(上下文上下文、AppWidgetManager、AppWidgetManager、int-appWidgetId、字符串标题、字符串文本){
Log.d(“MYTAG”,“在siglenote小部件的updatewidget方法中…”);
RemoteView视图=新的RemoteView(context.getPackageName(),R.layout.singlenote_小部件);
views.setTextViewText(R.id.single\u note\u title,title);
views.setTextViewText(R.id.single\u note\u text,text);
UpdateAppWidgetManager.UpdateAppWidgetId(appWidgetId,视图);
}
}
android清单中的内容:

  <receiver android:name=".SingleNote" 
                android:label="Y.A.N (single note widget)"
                android:enabled="@bool/is_GB">
        <intent-filter> 
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE"/> 
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
        <meta-data android:name="android.appwidget.provider" 
                   android:resource="@xml/singlenote_widget_provider" />
      </receiver>  

以下是我的两个主要问题:

  • 如何启动活动
  • 如何获取正在录制的小部件的小部件Id
    感谢您花时间阅读本文件以及您提供的任何帮助。

    我看不到清单中列出了您的活动,但您没有粘贴整个文件。确保活动存在于那里

    以下是我在许多小部件中用于启动活动的挂起意图的工作片段:

    Intent configIntent = new Intent(context, ActivityToBeLaunched.class);
            configIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            // Make the pending intent unique...
            configIntent.setData(Uri.parse(configIntent.toUri(Intent.URI_INTENT_SCHEME)));
            PendingIntent pendIntent = PendingIntent.getActivity(context, 0, configIntent, PendingIntent.FLAG_UPDATE_CURRENT);
            views.setOnClickPendingIntent(R.id.widgetBack, pendIntent);
        }
    

    还有一件事;我没有看到
    返回视图任何地方。您在其他地方处理吗?

    您是否在singlenote\u widget\u provider.xml中添加了
    android:configure
    项?您想从widget主屏幕启动
    activity
    ,即
    configure activity
    ?i、 e.单击主屏幕小部件打开
    配置活动
    是,我在小部件中列出了配置活动。\u provider.xmlno我不想启动小部件的配置活动,我想在应用程序中启动活动表单嘿,这是一个愚蠢的问题。但什么是回归观点?当我从一个小部件启动一个活动时,该活动只是启动并完成它的事情。对吗?添加configIntent.setData(Uri.parse(configIntent.tori(Intent.Uri\u Intent\u SCHEME));这张便条起了作用。