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

The method startActivity(Intent) is undefined for the type?(该类型的方法 startActivity(Intent) 未定义?)
本文介绍了该类型的方法 startActivity(Intent) 未定义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图通过按下一个小部件来启动一个活动.我一直遇到错误对于照片类型未定义方法 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) 未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)