谁(以及如何)创建AppWidgetProvider的实例?实例、AppWidgetProvider

2023-09-07 03:56:24 作者:少年,换心么

UPD2。这个问题已经某种魔法常规的走了之后(卸载干净Eclipse项目,重新启动Android设备,改造,安装,运行)。它不解决,它已经不见了。任何人都可以请解释发生了什么事?

upd2. The problem has gone after a sort of black magic routine (uninstall, clean Eclipse project, reboot Android device, rebuild, install, run). It is not solved, it has gone. Could anyone please explain what has happened?

UPD。看来,没有Java code运行在所有在我的Andr​​oid Widget项目您好Widget教程(参见注释)为蓝本。有没有什么办法可以运行一些code,使我的小工具有用吗?我相信这是可能的,因为有很多做出头的小部件。他们怎么做到的?

upd. Seems, no Java code running at all in my Android widget project modeled after Hello Widget tutorial (ref. comments). Is there any way to run some code to make my widget useful? I believe it is possible because there are a lot of do-something widgets. How they make it?

我模仿一个简单的Andr​​oid小工具您好Widget教程后: HTTP: //nm-blog.sanid.com/2009/07/android-hellowidget-tutorial/

I modeled a simple Android widget after Hello Widget tutorial: http://nm-blog.sanid.com/2009/07/android-hellowidget-tutorial/

运行它,我可以看到,它不工作在所有 - 。文中观点没有改变。

Running it I can see that it's not working at all - the text view is not altered.

下面是一些code:

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.pell.hellowidget"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="17" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >        
        <receiver android:name="HelloWidgetProvider" android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>
            <meta-data android:name="android.appwidget.provider"
                android:resource="@xml/hello_widget_provider" />
        </receiver>                
    </application>
</manifest>

RES / XML / hello_widget_provider.xml

res/xml/hello_widget_provider.xml

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
  android:minWidth="280dp"
  android:minHeight="180dp"
  android:updatePeriodMillis="10000"
  android:initialLayout="@layout/hello_widget_layout"
  android:resizeMode="vertical" />

RES /布局/ hello_widget_layout.xml

res/layout/hello_widget_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_vertical|center_horizontal"
    android:orientation="vertical" >
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

的src / COM /佩尔/ HelloWidget的/ HelloWidgetProvider.java:

src/com/pell/hellowidget/HelloWidgetProvider.java:

package com.pell.hellowidget;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Timer;
import java.util.TimerTask;

import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.widget.RemoteViews;

public class HelloWidgetProvider extends AppWidgetProvider {
    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new MyTime(context, appWidgetManager), 1,
                1000);

        super.onUpdate(context, appWidgetManager, appWidgetIds);
    }

    private Date currentTime;

    private class MyTime extends TimerTask {
        RemoteViews remoteViews;
        AppWidgetManager appWidgetManager;
        ComponentName thisWidget;
        DateFormat format = SimpleDateFormat.getTimeInstance(
                SimpleDateFormat.MEDIUM, Locale.getDefault());

        public MyTime(Context context, AppWidgetManager appWidgetManager) {
            this.appWidgetManager = appWidgetManager;
            remoteViews = new RemoteViews(context.getPackageName(),
                    R.layout.hello_widget_layout);
            thisWidget = new ComponentName(context, HelloWidgetProvider.class);
        }

        @Override
        public void run() {
            currentTime = new Date();
            remoteViews.setTextViewText(R.id.textView1,
                    format.format(currentTime));
            appWidgetManager.updateAppWidget(thisWidget, remoteViews);
        }
    }
}

据我的理解是不工作由于无人实例com.pell.hellowidget.HelloWidgetProvider对象。什么是Java / Android小工具的/ etc模拟为切入点(INT主(INT ARGC,CHAR *的argv []),WinMain函数等)?谁(以及如何)实例化对象AppWidgetProvider?

As far as I understand it is not working due to nobody instantiates com.pell.hellowidget.HelloWidgetProvider object. What is Java/Android Widget/etc analog for the entry point (int main(int argc, char * argv[]), WinMain, etc)? Who (and how) instantiates AppWidgetProvider object?

推荐答案

根据您的最终意见中。在的android:名称有时是必要的。作为一项规则,我总是在清单中使用完全限定域名(com.super.cool.thing.MySpecialClassActivity),因为它非常清楚。

As per your final comment, the . in the android:name can sometimes be essential. As a rule, I always use fully qualified name ("com.super.cool.thing.MySpecialClassActivity") in the manifest because it makes it very clear.

在切入点的AppWidget是提供程序类。然而,当它被调用,一旦的onUpdate (或的onDestroy )的回报,这个类是走了它只是实例化。虽然(我认为),你可以做你做了什么,也不会被认为是很好的做法 - 通常你应该创建一个定义单独的线程从管理窗口小部件,无论是服务,一个的AsyncTask ,一个等。

The "entry point" for an AppWidget is the provider class. However, it is only instantiated when it is called and, once onUpdate (or onDestroy) returns, the class is gone. Whilst (I think) you can do what you've done, it wouldn't be considered good practice - generally you should create a defined separate thread from which to manage your widget, either a Service, an AsyncTask, a Thread etc.

通常情况下,的onUpdate 方法将创建旨在启动一个服务类将控制部件的意图。这样的一个例子是在 http://www.vogella.com/articles/AndroidWidgets/article html的,特别是第8。我只在该教程一点狡辩:它一般建议不要使用updatePeriod。我也把一些注意事项在Service通过AlarmManager 被重新创建。

Typically, the onUpdate method will create an intent designed to start a Service class which will control the widget. An example of this is in http://www.vogella.com/articles/AndroidWidgets/article.html, section 8 in particular. I would only quibble with one point in that tutorial: it's generally advised to not use updatePeriod. I've also put some considerations for using Services and other things together in Service being re-Created by AlarmManager.