質問

Intent i = new Intent(context, MainActivity.class);
i.setAction(YOUR_AWESOME_ACTION);

PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
rootView.setOnClickPendingIntent(R.id.imgDistiFlag, pi);
appWidgetManager.updateAppWidget(widgetId, rootView);

and below is the code of onReceive function

if (intent.getAction().equals(YOUR_AWESOME_ACTION)) {

    Intent intn = new Intent (context, MainActivity.class);
    intn.setFlags (Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity (intn);

}
役に立ちましたか?

解決

You don't need that. For "just" opening an Activity from your widget class this will be enough:

Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);
remoteViews.setOnClickPendingIntent(R.id.imgDistiFlag, pendingIntent);

Edit for your wrong Manifest declaration:

Your Widget is not an Activity so you can't set it as Activity in manifest. You must use receiver.

    <receiver
        android:name="com.wapp.MyWidgetProvider"
        android:icon="@drawable/ic_launcher"
        android:label="lbl" >
        <intent-filter>
            <action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
        </intent-filter>

        <meta-data
            android:name="android.appwidget.provider"
            android:resource="@xml/widget_xml" />
    </receiver>

Your widget_xml file (dp values are for a 4x1 widget):

<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:minWidth="294dp"
    android:minHeight="72dp"
    android:updatePeriodMillis="1000"
    android:initialLayout="@layout/your_widget_layout_xml">
</appwidget-provider>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top