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

2023-09-09移动开发问题
3

本文介绍了该类型的方法 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) 未定义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何在 COCOS2d Android 中使用 CClistview?
How can I use CClistview in COCOS2d Android?(如何在 COCOS2d Android 中使用 CClistview?)...
2024-08-12 移动开发问题
5

cocos2d-android:如何显示分数
cocos2d-android: how to display score(cocos2d-android:如何显示分数)...
2024-08-11 移动开发问题
7

Sqlite 数据库未从资产文件夹 Android 复制
Sqlite database not copied from asset folder Android(Sqlite 数据库未从资产文件夹 Android 复制)...
2024-04-15 移动开发问题
8

SQLite 数据库副本在由设备而不是模拟器生成时出现损坏
SQLite Database Copy Appears Corrupted When Generated by Device and not Emulator(SQLite 数据库副本在由设备而不是模拟器生成时出现损坏)...
2024-04-15 移动开发问题
4

安卓文件拷贝
Android file copy(安卓文件拷贝)...
2024-04-15 移动开发问题
6

Android如何在android中检测Edittext的Copy事件
Android how to detect Copy event of Edittext in android(Android如何在android中检测Edittext的Copy事件)...
2024-04-15 移动开发问题
5