Android 锁定屏幕小部件设置问题
我正在开发一个在锁定屏幕窗口小部件区域显示我的应用程序的应用程序,因此我从中找到了一些示例,并尝试使用Android 锁定屏幕小部件设置问题,android,Android,我正在开发一个在锁定屏幕窗口小部件区域显示我的应用程序的应用程序,因此我从中找到了一些示例,并尝试使用AppWidgetProvider类处理服务,但当我在模拟器上运行它时,它不会在锁定屏幕上显示我的应用程序。 这是我的活动 ExampleAppWidgetProvider asd = new ExampleAppWidgetProvider(); AppWidgetManager appWidgetManager= AppWidgetManager.getInstance(ge
AppWidgetProvider
类处理服务,但当我在模拟器上运行它时,它不会在锁定屏幕上显示我的应用程序。
这是我的活动
ExampleAppWidgetProvider asd = new ExampleAppWidgetProvider();
AppWidgetManager appWidgetManager= AppWidgetManager.getInstance(getApplicationContext());
int[] appWidgetIds = null;
asd.onUpdate(getApplicationContext(), appWidgetManager,appWidgetIds);
示例AppWidgetProvider
private static final String LOG = "de.vogella.android.widget.example";
@Override
public void onUpdate(Context context, AppWidgetManager appWidgetManager,
int[] appWidgetIds) {
Log.w(LOG, "onUpdate method called");
// Get all ids
ComponentName thisWidget = new ComponentName(context,
ExampleAppWidgetProvider.class);
int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
// Build the intent to call the service
Intent intent = new Intent(context.getApplicationContext(),
UpdateWidgetService.class);
intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
// Update the widgets via the service
context.startService(intent);
}
UpdateWidgetService
public class UpdateWidgetService extends Service {
private static final String LOG = "de.vogella.android.widget.example";
@Override
public void onStart(Intent intent, int startId) {
Log.i(LOG, "Called");
// Create some random data
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this
.getApplicationContext());
int[] allWidgetIds = intent
.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
ComponentName thisWidget = new ComponentName(getApplicationContext(),
ExampleAppWidgetProvider.class);
int[] allWidgetIds2 = appWidgetManager.getAppWidgetIds(thisWidget);
Log.w(LOG, "From Intent" + String.valueOf(allWidgetIds.length));
Log.w(LOG, "Direct" + String.valueOf(allWidgetIds2.length));
for (int widgetId : allWidgetIds) {
// Create some random data
int number = (new Random().nextInt(100));
RemoteViews remoteViews = new RemoteViews(this
.getApplicationContext().getPackageName(),
R.layout.widget1);
Log.w("WidgetExample", String.valueOf(number));
// Set the text
remoteViews.setTextViewText(R.id.app_name,
"Random: " + String.valueOf(number));
// Register an onClickListener
Intent clickIntent = new Intent(this.getApplicationContext(),
ExampleAppWidgetProvider.class);
clickIntent.setAction(AppWidgetManager.ACTION_APPWIDGET_UPDATE);
clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS,
allWidgetIds);
PendingIntent pendingIntent = PendingIntent.getBroadcast(
getApplicationContext(), 0, clickIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
remoteViews.setOnClickPendingIntent(R.id.movie_name, pendingIntent);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
stopSelf();
super.onStart(intent, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
根据我对一个类似问题的回答:
- 更新
以包括widgetCategory
keyguard
- 添加
initialKeyguardLayout
/res/xml/widget_info.xml
文件中完成,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:initialKeyguardLayout="@layout/widget"
android:initialLayout="@layout/widget"
android:minHeight="40dp"
android:minWidth="250dp"
android:updatePeriodMillis="0"
android:widgetCategory="home_screen|keyguard" >
</appwidget-provider>
看看另一个答案——它包含指向一个非常简单的演示小部件的链接,您可以从MyGitHub获得这个小部件。他们可能会帮助你,即使他们与这个问题并不完全相关