2016-06-29 1 views
1

Salut, j'utilise un widget pour mon application avec un simple texte, mais lorsque je tape sur mon widget, mon application n'est pas lancée.Application non démarrée en appuyant sur le widget

est ici les données

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> 
fichier manifeste

:

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

Répondre

1

On dirait que vous n'avez pas de récepteur pour votre PendingIntent.

Vous pouvez appeler PendingIntent.getActivity() au lieu de PendingIntent.getBroadcast() pour lancer StockActivity.

la documentation De PendingIntent.getActivity():

récupérer un PendingIntent qui va commencer une nouvelle activité, comme appeler Context.startActivity(Intent). Notez que l'activité commencera en dehors du contexte d'une activité existante, vous devez donc utiliser l'indicateur de lancement Intent.FLAG_ACTIVITY_NEW_TASK dans le Intent.

+0

Merci ... qui a résolu le problème –