该类型的方法 startActivity(Intent) 未定义?

发布于 2024-11-29 08:10:56 字数 390 浏览 0 评论 0原文

所以我试图通过按一个小部件来启动一个活动。我不断遇到错误“照片类型的方法 startActivity(Intent) 未定义”,非常感谢任何帮助!我的班级代码如下:

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

}

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

北陌 2024-12-06 08:10:56

您需要在 AppWidgetProvider 类中实现 onUpdate() 和 onReceive() 方法,如下所示:

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() 方法来接收挂起的意图并启动相关活动:

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

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

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);
    }
}
弄潮 2024-12-06 08:10:56

你用什么方法把这段代码放进去的? AppWidgetProvider 没有 startActivity() 方法。为此,您需要一个Context。多个 AppWidgetProvider 的回调方法可让您访问 Context 对象,但这实际上完全取决于您打算执行的操作。

我建议您在此处。

What method have you put this code in? AppWidgetProvider does not have a startActivity() method. You need a Context for that. Several of AppWidgetProvider's callback methods give you access to a Context object, but it all really depends on what you intend to do.

I suggest you take a look at the Android developer guide's documentation on AppWidgets here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文