我想从appwidget弹出一个错误,当它的后台任务之一失败,但我不认为这是允许的 - 或者,如果它的工作原理,它是安全的.
我确定我错过了一些东西,但我只是想用一个按钮和一个计数器来获取一个app小部件.每次我点击按钮,我都希望计数器更新1.
我已经设置了WidgetProvider的onUpdate()函数来向按钮注册一个挂起的事件,以便它启动一个服务来碰撞计数器numbver:
Intent active = new Intent(context, CounterService.class);
active.setAction(CounterService.COUNT);
PendingIntent pending = PendingIntent.getService(context, 0, active, PendingIntent.FLAG_UPDATE_CURRENT);
views.setOnClickPendingIntent(R.id.CountButton, pending);
ComponentName component = new ComponentName(context.getPackageName(), KickCounterWidgetProvider.class.getName());
appWidgetManager.updateAppWidget(component, views);
Run Code Online (Sandbox Code Playgroud)
然后在服务CounterService :: onStart()函数中我计算计数(暂时存储在prefs中),然后尝试更新显示当前计数值的文本字段:
// ... bump the count here and store a string representation of it in currentCountString ...
RemoteViews remoteView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.widget);
remoteView.setTextViewText(R.id.CurrentKickCount, currentCountString);
// apply changes to widget
AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(getApplicationContext());
ComponentName component = new ComponentName(getApplicationContext().getPackageName(), KickCounterWidgetProvider.class.getName());
appWidgetManager.updateAppWidget(component, remoteView);
Run Code Online (Sandbox Code Playgroud)
明智地使用Logcat表示这一切都正常,字符串似乎没问题,但由于某种原因,对appWidgetManager.updateAppWidget()的调用似乎由于某种原因而无声地失败.
我不知道它是否相关,但是我第一次将小部件的实例添加到主屏幕,甚至按钮都不起作用(即,在提供程序中的onUpdate()中调用updateAppWidget()失败).小部件的后续实例似乎可以在提供程序中调用updateAppWidget(),但从不适用于该服务.
任何帮助将不胜感激.
我已经想出如何使用setOnClickPendingIntent来检测和处理视图上的按下,但我找不到任何方法来检测长按(或双击).我想根据用户是按下还是长按来执行不同的操作.这可能吗?
谢谢.
我想用
private static final Random rgenerator = new Random();
Resources res = getResources();
myString = res.getStringArray(R.array.xmlString);
String q = myString[rgenerator.nextInt(myString.length)];
Run Code Online (Sandbox Code Playgroud)
当我在一个活动中使用相同它工作正常,但如果我在AppWidget类中使用它会抛出一个错误getResources();是否有任何方法在AppWidget中使用它,基本上我想要的是获取随机xml字符串到appWidget,是否有任何解决这个问题?
我添加android:exported="true"标签后,我在清单文件中收到了AppWidget配置活动的警告.这就是它的样子......
<activity android:name=".widgets.WidgetConfigurationActivity"
android:theme="@android:style/Theme.Translucent"
android:exported="true" >
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
Run Code Online (Sandbox Code Playgroud)
我得到的警告是"导出的活动不需要许可".根据我的理解,将exported标记设置为true意味着与我的应用程序无关的另一个应用程序可以访问它(这将是有意义的,因为主屏幕启动器应用程序需要启动我的AppWidget的配置活动).有谁知道我需要在这里添加什么样的权限才能使这不会出错?
我想获取与 appwidget id 关联的 appwidget 布局。
在 appwidget 的 Widget provider 类中,android 文档提供了一种简单的方法来获取 appwidget id...
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
final int N = appWidgetIds.length;
// Perform this loop procedure for each App Widget that belongs to this provider
for (int i=0; i<N; i++) {
int appWidgetId = appWidgetIds[i];
Run Code Online (Sandbox Code Playgroud)
但是它们的 ID 不允许我知道它来自哪个 appwidget 提供程序(多个小部件提供程序和布局。)。
编辑:我使用服务类来更新小部件,而不是小部件提供程序类。
在服务类中,我想获取每个 id 的 appwidget 布局,以便我可以使用 remoteviews 来更新该布局。我尝试了一些不同的建议,但没有一个给我需要与远程视图一起使用的价值。例如“R.id.widget_layout”
我怎样才能以正确的“int”形式从appwidget ID中得到一个像“R.id.widget_layout”这样的值来与remoteviews一起使用?
我尝试创建一个应该占据整个屏幕宽度的小部件。但它最初总是对应于android:minWidth。
当然,我可以将 android:minWidth 设置为 250 dp,但我想保持小部件可调整大小。
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
android:minWidth="170dp"
android:minHeight="40dp"
android:updatePeriodMillis="86400000"
android:previewImage="@drawable/widget_preview"
android:initialLayout="@layout/widget_4"
android:configure="test.WidgetConfigureActivity"
android:resizeMode="horizontal"
android:widgetCategory="home_screen"
android:initialKeyguardLayout="@layout/widget_4">
</appwidget-provider>
Run Code Online (Sandbox Code Playgroud)
布局:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/widget_padding"
android:clickable="true"
android:focusable="true"
android:background="@color/translucent_black">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:baselineAligned="false">
<RelativeLayout
android:orientation="horizontal"
android:layout_marginLeft="10dp"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageButton
android:id="@+id/imageCircleButton"
android:layout_gravity="center_vertical"
android:layout_width="52dp"
android:layout_height="52dp"
android:scaleType="fitXY"
android:src="@drawable/widget_circle_w"
android:background="@android:color/transparent"
android:layout_centerVertical="true"
android:contentDescription="@string/strLevelIndicator"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/appwidget_text_level"
android:textStyle="bold"
android:textSize="30sp"
android:layout_gravity="center_vertical"
android:textColor="#ffffff"
android:text="@string/strWidgetDefLevel"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:gravity="clip_vertical"
android:layout_weight="0.8"
android:layout_gravity="left|center_vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/appwidget_text_map" …Run Code Online (Sandbox Code Playgroud) android widget android-widget android-layout android-appwidget
我正在尝试从标准日志切换到Timber,以便在将我的应用部署到Google Play时轻松删除日志消息,但它无法正常工作
的build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "org.thingdroid"
minSdkVersion 16
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
}
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.1.3'
}
}
repositories {
mavenCentral()
}
dependencies {
compile 'com.jakewharton.timber:timber:3.1.0'
}
Run Code Online (Sandbox Code Playgroud)
ThingDroid.java
package org.thingdroid;
...
import timber.log.Timber;
import static timber.log.Timber.DebugTree;
public class ThingDroid extends AppWidgetProvider {
@Override
public void onEnabled(Context context) {
super.onEnabled(context);
if (MyBuildConfig.DEBUG) {
Timber.plant(new DebugTree());
} else {
//Timber.plant(new CrashReportingTree());
} …Run Code Online (Sandbox Code Playgroud) 到目前为止,我一直在RemoteViews使用remoteViews.setImageViewBitmap(). 它总体上运行良好。
但是一些用户遇到了问题,我认为这是在加载非常大的位图时。无论如何,我已经将位图缓存到本地存储,所以我的想法是改用setImageViewUri()其他地方的建议。
但我无法让它工作......我只是得到一个空白的小部件。以下代码段说明了我在做什么(省略了一些上下文,但希望留下足够的内容)...
// Bitmap bmp is fetched from elsewhere... and then...
FileOutputStream fos = context.openFileOutput("img.png", Context.MODE_PRIVATE);
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.flush();
fos.close();
// ... later ...
// this works...
remoteViews.setImageViewBitmap(R.id.imageView, bmp);
// but this doesn't...
remoteViews.setImageViewUri(R.id.imageView, Uri.fromFile(new File(context.getFilesDir().getPath(), "img.png")));
Run Code Online (Sandbox Code Playgroud)
编辑
尝试FileProvider按照以下 CommonsWare 的建议使用...
显现:
<provider
android:name="android.support.v4.content.FileProvider"
android:authorities="com.xyz.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
Run Code Online (Sandbox Code Playgroud)
xml/file_paths.xml:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="cache" path="."/>
</paths>
Run Code Online (Sandbox Code Playgroud)
代码:
File imagePath = new File(context.getFilesDir(), ".");
File newFile = …Run Code Online (Sandbox Code Playgroud) 由于 Android 团队在“此处”发布了新的后台服务限制,因此通过在 Android O 中的 Widget(主屏幕)按钮上设置单击 Pending Intent 来启动 Intent 服务时出现问题。我的问题是:如何添加新的通过单击小部件按钮将任务发送到 JobScheduler?
android ×10
java ×1
long-press ×1
manifest ×1
permissions ×1
random ×1
remoteview ×1
resources ×1
toast ×1
widget ×1