嗨,我正在为我的应用程序使用一个带有简单文本的小部件,但是当我点击我的小部件时,我的应用程序未启动。
这是数据
WidgetProvider.java
public class StockWidgetProvider extends AppWidgetProvider {
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds);
for (int widgetId : appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(context.getPackageName(),
R.layout.widget_layout);
// Register an onClickListener
Intent intent = new Intent(context, StockActivity.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(context,
0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.widget, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
}
}
widget_provider_info.xml:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minHeight="50dp"
android:minWidth="150dp"
android:resizeMode="horizontal|vertical"
android:updatePeriodMillis="0"
android:initialLayout="@layout/widget_layout"
android:widgetCategory="home_screen"
android:previewImage="@drawable/widget_image">
</appwidget-provider>
widget_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/widget"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:background="@drawable/widget_shape" >
<TextView
android:id="@+id/update"
style="@android:style/TextAppearance.Medium"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center_horizontal|center_vertical"
android:layout_margin="4dip"
android:text="Static Text" >
</TextView>
</LinearLayout>
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.stockhawk">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".ui.StockActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".service.StockService"
android:exported="false" />
<service android:name=".service.DetailService"
android:exported="false"/>
<provider
android:name=".data.StockProvider"
android:authorities="com.example.android.stockhawk.data.StockProvider"
android:exported="false" />
<activity
android:name=".ui.DetailActivity"
android:label="@string/title_activity_detail"
android:parentActivityName=".ui.StockActivity"
android:theme="@style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.android.stockhawk.ui.StockActivity" />
</activity>
<service
android:name=".service.DetailService"
android:exported="false"/>
<receiver android:name=".extras.StockWidgetProvider"
android:exported="true">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/widget_provider_info" />
</receiver>
</application>
</manifest>
最佳答案
似乎您的PendingIntent
没有接收器。
您可能要调用PendingIntent.getActivity()
而不是PendingIntent.getBroadcast()
来启动StockActivity
。
从PendingIntent.getActivity()
的文档中:
检索将启动新活动的PendingIntent
,例如调用
Context.startActivity(Intent)
。请注意,活动将开始
在现有活动的上下文之外,因此您必须使用
Intent.FLAG_ACTIVITY_NEW_TASK
中的Intent
启动标志。