我遇到了路障。我正在进行一系列的力量关闭。当我在模拟器中运行我的程序时,它会在我放置小部件之前在安装时关闭。
基本上我已经尝试制作一个有按钮的小部件。该按钮用于启动服务以将窗口小部件更改为新布局(菜单)。 还有一个配置活动。
如果我需要添加更多信息,请告诉我。我一直在使用android开发页面和Lars Vogels教程作为参考。 http://www.vogella.de/articles/AndroidWidgets/article.html
谢谢 RIP SJ。我从来不是一个mac-ie但是我很感激你的工作。
提供者:
public class CodeSummitWidgetProvider extends AppWidgetProvider{
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
Intent intent = new Intent(context.getApplicationContext(), TestService.class);
PendingIntent pendingIntent = PendingIntent.getService(context.getApplicationContext(), 0, intent, 0);
RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.codesummit_widget_provider_layout);
views.setOnClickPendingIntent(R.id.tomenu, pendingIntent);
appWidgetManager.updateAppWidget(appWidgetIds, views);
context.startService(intent);
}
}
服务:
public class TestService extends Service {
@Override
public void onStart(Intent intent, int startId) {
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(this.getApplicationContext());
int[] appWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
if (appWidgetIds.length > 0) {
for(int widgetId : appWidgetIds) {
RemoteViews remoteViews = new RemoteViews(getPackageName(), R.layout.codesummit_widget_menu_layout);
appWidgetManager.updateAppWidget(widgetId, remoteViews);
}
stopSelf();
}
super.onStart(intent, startId);
}
The Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.main.codesummit"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<receiver android:name="CodeSummitWidgetProvider">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data android:name="android.appwidget.provider"
android:resource="@xml/codesummit_appwidget_info" />
</receiver>
<activity android:name=".CodeSummitWidgetConfigure">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<service android:name=".TestService" />
</application>
LogCat:
10-06 23:59:04.925: INFO/ActivityManager(65): Start proc com.main.codesummit for broadcast com.main.codesummit/.CodeSummitWidgetProvider: pid=312 uid=10036 gids={}
10-06 23:59:05.596: DEBUG/AndroidRuntime(312): Shutting down VM
10-06 23:59:05.606: WARN/dalvikvm(312): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): FATAL EXCEPTION: main
10-06 23:59:05.665: ERROR/AndroidRuntime(312): java.lang.RuntimeException: Unable to start service com.main.codesummit.TestService@44ee5a38 with Intent { cmp=com.main.codesummit/.TestService }: java.lang.NullPointerException
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3063)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at android.app.ActivityThread.access$3600(ActivityThread.java:125)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2096)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at android.os.Handler.dispatchMessage(Handler.java:99)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at android.os.Looper.loop(Looper.java:123)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at java.lang.reflect.Method.invokeNative(Native Method)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at java.lang.reflect.Method.invoke(Method.java:521)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at dalvik.system.NativeStart.main(Native Method)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): Caused by: java.lang.NullPointerException
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at com.main.codesummit.TestService.onStart(TestService.java:23)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at android.app.Service.onStartCommand(Service.java:420)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3053)
10-06 23:59:05.665: ERROR/AndroidRuntime(312): ... 10 more
10-06 23:59:10.696: INFO/Process(312): Sending signal. PID: 312 SIG: 9
10-06 23:59:10.795: INFO/ActivityManager(65): Process com.main.codesummit (pid 312) has died.
答案 0 :(得分:1)
在onUpdate方法中,您在一些地方使用了错误的上下文。 传递给onUpdate的上下文仅在onUpdate函数完成执行之前一直存在。所以你不能将它用于你想要活得更久的任何东西,然后运行这个功能,例如你的服务。应用程序上下文适用于应用程序的生命周期。要从onUpdate调用context.getApplicationContext()。
所以你需要改变这些方面的背景:
Intent intent = new Intent(context, TestService.class);
PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, 0);
要:
Intent intent = new Intent(context.getApplicationContext(), TestService.class);
PendingIntent pendingIntent = PendingIntent.getService(context.getApplicationContext(), 0, intent, 0);
此类的访问级别也不正确
class TestService extends Service {
将此作为公共课程。由于它不公开,操作系统在尝试启动服务时无法使用它。