【问题标题】:Widget : Activity automatically launched without onclick()小部件:无需 onclick() 即可自动启动 Activity
【发布时间】:2013-02-05 00:03:18
【问题描述】:

如果单击小部件,我正在尝试打开一个活动。但是,当我将包上传到我的模拟器时,活动会自动打开,即使它只有在我单击小部件时才会打开。

这是我的清单文件。

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.NewsWidget"
    android:versionCode="1"
    android:versionName="1.0" >

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <service android:name=".UpdateWidgetService" >
        </service>

        <receiver android:name="MyWidgetProvider" >
            <intent-filter>
                <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
            </intent-filter>

            <meta-data
                android:name="android.appwidget.provider"
                android:resource="@xml/widget_info" />
        </receiver>
         <activity
            android:name="com.example.NewsWidget.MainActivity"
            android:label="@string/app_name"
            android:launchMode="singleInstance" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".DetailedNewsViewer"
            android:label="DetailedNewsViewer" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-sdk android:minSdkVersion="8" />

    <uses-permission android:name="android.permission.INTERNET" />

</manifest>

我已使用此处另一个问题 (Launching activity from widget) 中的详细信息将以下代码添加到我的小部件提供程序中。

 Intent i = new Intent(); 
        i.setClassName("com.example.NewsWidget", "com.example.NewsWidget.MainActivity"); 
        PendingIntent myPI = PendingIntent.getService(context, 0, i, 0); 
        //intent to start service 

      // Get the layout for the App Widget 
      RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout); 

      //attach the click listener for the service start command intent 
      views.setOnClickPendingIntent(R.id.update, myPI); 

      //define the componenet for self 
      ComponentName comp = new ComponentName(context.getPackageName(), MyWidgetProvider.class.getName()); 

      //tell the manager to update all instances of the toggle widget with the click listener 
      mgr.updateAppWidget(comp, views); 

有什么问题?

【问题讨论】:

  • 我假设您使用的是 Eclipse?如果是这样,这就是应该发生的事情!它会为您启动应用程序。尝试按主页按钮,然后测试您的小部件。如果你想 100%,试着去掉小部件代码并运行它,应用程序仍然会启动!
  • @edthethird 嗨,如果我打开小部件然后单击它,应用程序不会打开。但是,当我从 Eclipse 编译/运行时它会打开。
  • 您也可以发布widget_layout的内容吗?那里可能是个问题,因为我在这里看不到任何东西。

标签: android android-intent android-widget


【解决方案1】:

我注意到您正在使用PendingIntent.getService(),但您调用的是活动而不是服务(根据您的清单)。尝试改用 PendingIntent.getActivity() 并查看您的 pendingIntent 是否触发。

当您从 Eclipse 运行时,您的主要活动正在加载,但 edthethird 是正确的,这可能与您的问题无关。这只是启动主要活动的默认 Eclipse 行为。

【讨论】:

    猜你喜欢
    • 2021-02-05
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    • 2015-08-10
    相关资源
    最近更新 更多