android - 小工具 : Activity automatically launched without onclick()

标签 android android-intent android-widget

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

这是我的 list 文件。

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

问题是什么?

最佳答案

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

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

关于android - 小工具 : Activity automatically launched without onclick(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14697994/

相关文章:

java - 获取android中所有已安装应用的名称、图标和包名

android - Aniqroid SlidingTray NullReferenceException?

java - 如何使用 RemoteViews 更改小部件背景?

android - 我应该在 Android Market 中使用多大尺寸的屏幕截图?

android - 为什么 Android 收不到 FCM 推送通知?

java - Android Studio - 如何修复使用 flag_activity_clear_task 后后退按钮仍然有效的问题 | flag_activity_new_task?

java - Android:无法从扩展Thread的类中调用新 Activity

android - 如何限制 DatePicker 上的用户只能选择 today_date 之前的日期 - 18 年

android - 无法从收件箱中读取所有短信

java - 为什么 Log.i 在绘制时(位于 SquashCourtView 类下)在 drawCourt() 方法下不记录任何内容?