所以我试图通过按下一个小部件来启动一个活动.我一直遇到错误对于照片类型未定义方法 startActivity(Intent)",非常感谢任何帮助!我的课程代码如下:
So im trying to start an activity by pressing a widget. I keep running into the error "The method startActivity(Intent) is undefined for the type Photos" any help is much appreciated! My class code is below:
package com.natehoch96.widgets.iOS;
import android.appwidget.AppWidgetProvider;
import android.content.Intent;
public class Photos extends AppWidgetProvider {
Intent myIntent = new Intent(android.provider.Settings.ACTION_SETTINGS);
{startActivity(myIntent); }
}
您需要在 AppWidgetProvider 类中实现 onUpdate() 和 onReceive() 方法,如下所示:
You need to implement the onUpdate() and onReceive() methods in your AppWidgetProvider class, something like this:
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
//Get the views for your widget layout
RemoteViews views = new RemoteViews(context.getPackageName(),
R.layout.my_widget);
//Create a pending intent for a widget click
Intent intent = new Intent(context, Photos.class);
intent.setAction("PhotosAction");
PendingIntent pendingIntent =
PendintIntent.getBroadcast(context, 0, intent, 0);
views.setOnClickPendingIntent(R.id.widget_click_view, pendingIntent);
appWidgetManager.updateAddWidget(appWidgetId, views);
}
}
然后编写你的 onReceive() 方法来接收待处理的意图并启动相关活动:
Then write your onReceive() method to receive the pending intent and start the relevant activity:
public void onReceive(Context context, Intent intent) {
super.onReceive(context, intent);
if (intent.getAction().equals("PhotosAction") {
//Received photos action action, start your target activity
Intent i = new Intent(android.provider.Settings.ACTION_SETTINGS);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
这篇关于该类型的方法 startActivity(Intent) 未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!