1

我无法在 android 设备的锁定屏幕上显示小部件,我的代码:

    package com.example.widgetapp;

    import android.annotation.SuppressLint;
    import android.app.PendingIntent;
    import android.appwidget.AppWidgetManager;
    import android.appwidget.AppWidgetProvider;
    import android.content.ActivityNotFoundException;
    import android.content.ComponentName;
    import android.content.Context;
    import android.content.Intent;
    import android.net.Uri;
    import android.view.WindowManager;
    import android.widget.RemoteViews;
    import android.widget.Toast;

public class MainActivity extends AppWidgetProvider {

    // private static final String ACTION_CLICK = "ACTION_CLICK";

     @SuppressLint("NewApi")
    public void onUpdate(Context context, AppWidgetManager appWidgetManager,
              int[] appWidgetIds)
     {


         ComponentName thisWidget = new ComponentName(context,
                    MainActivity.class);
         int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
         for (int widgetId : allWidgetIds) {

                  try {
//                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
//                    getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
                      Intent intent = new Intent("android.intent.action.MAIN");
                      intent.addCategory("android.intent.category.LAUNCHER");

                      intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                      intent.setComponent(ComponentName.unflattenFromString("com.example.rage/com.example.rage.RageActivity"));

                      Intent callIntent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:"+198)); 
                         callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

                         //remoteViews.setOnClickFillInIntent(widgetId, callIntent);

                      PendingIntent pendingIntent = PendingIntent.getActivity(
                          context, 0, intent, 0);
                      PendingIntent pendingIntent1 = PendingIntent.getActivity(
                              context, 0, callIntent, 0);
                      RemoteViews views = new RemoteViews(context.getPackageName(),
                              R.layout.activity_main);
//                    views.setOnClickFillInIntent(widgetId, intent);
//                    views.setOnClickFillInIntent(widgetId, callIntent);

                     // context.startActivity(intent);
                    //  context.startActivity(callIntent);

                      views.setOnClickPendingIntent(R.id.button1, pendingIntent1);
                      views.setOnClickPendingIntent(R.id.button2, pendingIntent);

                  appWidgetManager.updateAppWidget(widgetId, views);

                  } catch (ActivityNotFoundException e) {
                          Toast.makeText(context.getApplicationContext(),
                                  "There was a problem loading the application: ",
                                  Toast.LENGTH_SHORT).show();

                  }
         }

}
4

1 回答 1

1

尝试将:“android:widgetCategory="home_screen|keyguard" 添加到提供程序 xml

<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
    android:initialLayout="@layout/my_widget_layout"
    android:minHeight="40dp"
    android:minWidth="40dp"
    android:resizeMode="horizontal|vertical"
    android:widgetCategory="home_screen|keyguard" >
</appwidget-provider>
于 2013-06-27T23:30:45.897 回答