小编Art*_*aev的帖子

两种方法之间更新Android App Widget有什么区别?

我在网上/书中读到了一些关于App Widget的例子,更新小部件的正常例子是onUpdate(Context context, AppWidgetManager appWidgetManager,int[] appWidgetIds)AppWidgetProvider的方法,如下所示:

final int N = appWidgetIds.length;
for (int i=0; i<N; i++) {
 int appWidgetId = appWidgetIds[i];
 RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.mywidget_layout);
 updateViews.setTextViewText(R.id.mytext, "updated text");

 appWidgetManager.updateAppWidget(appWidgetId, updateViews);    
}
Run Code Online (Sandbox Code Playgroud)

它会循环更新每个小部件.

但是现在,我必须实现一个App Widget,它在BroadcastReceiver onReceive(Context context, Intent intent)方法中更新,因为没有传入int [] appWidgetIds.所以我实现了这样的代码:

RemoteViews updateViews = new RemoteViews(context.getPackageName(), R.layout.mywidget_layout);
updateViews.setTextViewText(R.id.mytext, "updated text");

ComponentName myComponentName = new ComponentName(context, AndroidBatteryWidgetProvider.class);
AppWidgetManager manager = AppWidgetManager.getInstance(context);
manager.updateAppWidget(myComponentName, updateViews);
Run Code Online (Sandbox Code Playgroud)

它没有逐个更新小部件,但实际上所有小部件都立即更新.即使它按我的意愿工作,但我很困惑为什么不需要像以前一样逐个更新所有小部件.

两种方法有什么区别?

我可以发送另一个广播BroadcastReceiver.onReceive()来触发AppWidgetProvider.onUpdate()吗?怎么样?

android widget broadcastreceiver android-appwidget

5
推荐指数
1
解决办法
832
查看次数

在WebView中将一组值从Android Activity传递到JavaScript

我正在使用JS Charts库在我的Android应用程序的WebView中绘制图形.我想提供SQLite数据库中的数据.此时我仍然坚持如何将数据从Java传递到JavaScript.JavaScript部分需要这样的东西:

data = new Array([10, 10], [20, 10]);
Run Code Online (Sandbox Code Playgroud)

我知道addJavaScriptInterface并且我设法将单个值从我的Activity传递给WebView.这只是阵列给我带来麻烦.我想到了类似的东西:

final class ChartDataLoader {

    public double[][] getData() {
        double[][] data = {{10, 10}, {20, 10}};
        return data;
    }
}
Run Code Online (Sandbox Code Playgroud)

请注意,目前我只是对数据进行硬编码,但最终会从数据库中提取出来.那么我把它暴露给我的JS:

webView.addJavascriptInterface(new ChartDataLoader(), "dataLoader");
Run Code Online (Sandbox Code Playgroud)

最后尝试用JavaScript阅读:

<html>
<head>
<script type="text/javascript" src="jscharts.js"></script>
</head>

<body>
<div id="chartcontainer">You should see a chart here.</div>

<script type="text/javascript">

 myData = dataLoader.getData(); 
 alert("DataReceived: " + myData.length);
 alert("Element 0 : " + myData[0]);

 var myChart = new JSChart('chartcontainer', 'line');
 myChart.setDataArray(myData);
 myChart.draw();

</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)

JavaScript在这两个警告声明中失败,说明:

错误/ Web控制台(2455):未捕获的TypeError:无法读取文件中未定义的属性"长度":///android_asset/chart.html:15

任何提示?我在网上看到了一些代码,其他人将数组转换为String,然后用JavaScript重新创建它,但这似乎对我来说太过分了,我希望有更好的解决方案.另一种方法是将XML文件传递给图表库,但同样,我认为每次用户想要查看图表时创建新XML都会很慢.

javascript java android android-webview

4
推荐指数
1
解决办法
6831
查看次数

使用Android从服务器获取数据并加载新屏幕

我正在使用以下代码从服务器获取数据,如果来自服务器的值为,GEEK则它将加载至下一个类,但不会加载新视图。你能说出什么问题吗?

    公共无效onCreate(捆绑保存的InstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        登录=(按钮)findViewById(R.id.login);
        用户名=(EditText)findViewById(R.id.username);
        密码=(EditText)findViewById(R.id.password);

        login.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v){
字符串Re;
                字符串mUsername = username.getText()。toString();
                字符串mPassword = password.getText()。toString();

                Re = tryLogin(mUsername,mPassword);

                if(Re ==“ GEEK”)
                {
                    意图i = new Intent();
                    i.setClassName(v.getContext(),“ com.httplogin.MainScreen”);
                    startActivity(i);

                }
            }
        });
    }

    受保护的String tryLogin(String mUsername,String mPassword)
    {           
        HttpURLConnection连接;
       OutputStreamWriter request = null;

            URL url = null;   
            字符串响应= null;         
            字符串参数=“ username =” + mUsername +“&password =” + mPassword;   

            尝试
            {
                url =新的URL(“ http://serverspace/script.php”);
                连接=(HttpURLConnection)url.openConnection();
                connection.setDoOutput(true);
                connection.setRequestProperty(“ Content-Type”,“ application / x-www-form-urlencoded”);
                connection.setRequestMethod(“ POST”); …

android

4
推荐指数
1
解决办法
3058
查看次数

Android RTL布局方向对齐中心问题

我正在开发一个需要支持阿拉伯语的Android应用程序.(应该从右到左阅读).在快速搜索解决方案后,我发现android完全支持阿拉伯语言本身在API级别17中的声明

机器人:supportsRtl = "真"

在AndroidManifest里面的应用程序标签中,这样我就可以使用布局镜像来自动翻转布局,从而获得更好的从右到左的阅读体验.但是,我注意到在布局镜像期间在子RelativeLayout内部的视图中使用centerInParent时会出现问题.以下是我的代码和预期的布局.

<RelativeLayout
    android:background="@color/white"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:padding="20dp">

    <RelativeLayout
        android:background="@drawable/shape_flag_imageview_boarder"
        android:id="@+id/imageLayout"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">

        <ImageView
            android:id="@+id/image"
            android:layout_height="100dp"
            android:layout_width="100dp"
            android:visibility="invisible" />

        <ProgressBar
            android:id="@+id/progressbar"
            android:layout_centerInParent="true"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content" />

    </RelativeLayout>

    <TextView
        android:id="@+id/text"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="10dp"
        android:layout_toEndOf="@id/imageLayout"
        android:layout_width="wrap_content"
        android:text="Some text here bla bla bla"
        android:textColor="@color/black" />

</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

从左到右的正常布局

上图显示了从左到右的正常布局方向的预期结果.我在一个子视图中将ImageView和ProgressBar包装在一起的目的是因为我希望在从Internet加载图像时,在ImageView中间显示ProgressBar.在我将Locale更改为阿拉伯语后,它变得像 在RTL中布局
正如我尝试和错误并弄清楚这是由ProgressBar的centerInParent引起的.它不是以子视图为中心,而是将中心与根外父视图对齐,后者是最外层的RelativeLayout.下面是从ProgressBar中删除centerInParent代码的屏幕截图.

在此输入图像描述

它清楚地显示布局镜像效果很好,但ProgressBar位置不是我所期望的.所以我尝试使用centerVertical和centerHorizo​​ntal,结果分别显示在下面的图像中. 在此输入图像描述 在此输入图像描述

这些解决方案都不起作用,我搜索过的主题都与此问题无关.所以我猜这可能是Android库中的一个错误?如果有人知道问题或解决方案,请与我分享.谢谢

android alignment arabic android-layout right-to-left

4
推荐指数
1
解决办法
2857
查看次数

无法同步Gradle项目 - Android Studio 2.2

我最近更新到Android Studio 2.2并将Gradle插件版本更改为2.2.0 com.android.tools.build:gradle:2.2.0.

现在,我在尝试同步Gradle文件时收到以下错误:

Gradle sync失败:无法加载类'com.android.builder.profile.Recorder $ Property'.

如果我将Gradle插件版本更改为2.1.3,问题就会消失.我如何使用最新版本2.2.0?

PS我也在使用gradle-experimental来获得NDK支持 - com.android.tools.build:gradle-experimental:0.7.3

android android-ndk android-gradle-plugin gradle-plugin

3
推荐指数
1
解决办法
8894
查看次数

不调用来自BroadcastReceiver的onReceive

我有一个音乐应用程序,我试图在通知栏上添加一些操作按钮.

我试过这样的事情:

 public void onPrepared(MediaPlayer mediaPlayer) {
    mediaPlayer.start();
    Intent onPreparedIntent=new Intent("MEDIA_PLAYER_PREPARED").putExtra("CURR_SONG",songposn);
    LocalBroadcastManager.getInstance(this).sendBroadcast(onPreparedIntent);
    Intent notintent = new Intent(this, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    Notification.Builder builder=new Notification.Builder(this);
    PendingIntent pendingIntent=PendingIntent.getActivity(this,0,notintent,PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent prevPendingIntent=PendingIntent.getActivity
            (this,1,new Intent().setAction("PREVIOUS"),PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent pausePendingIntent=PendingIntent.getActivity
            (this,2,new Intent().setAction("PAUSE"),PendingIntent.FLAG_UPDATE_CURRENT);
    PendingIntent nextPendingIntent=PendingIntent.getActivity
            (this,3,new Intent().setAction("NEXT"),PendingIntent.FLAG_UPDATE_CURRENT);;
    builder.setContentIntent(pendingIntent).setSmallIcon(R.drawable.playicon)
            .addAction(R.drawable.back, "Previous", prevPendingIntent)
            .addAction(R.drawable.playsmall, "Pause", pausePendingIntent)
            .addAction(R.drawable.forw, "Next", nextPendingIntent)
            .setTicker(songArtist)
            .setOngoing(true).setContentTitle(songTitle).setContentText(songArtist);

    Notification not=builder.build();
    startForeground(MusicService.NOTIFY_ID,not);

}
Run Code Online (Sandbox Code Playgroud)

NotificationReciever在这项服务中宣布了一个班级

public class NotificationReciever extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        Log.e("here","here");
       String action=intent.getAction();
       if(action!=null){
           switch (action){
               case "PREVIOUS":{
                   playPrev();
                   break;
               }
               case "PAUSE":{ …
Run Code Online (Sandbox Code Playgroud)

java android action broadcastreceiver

3
推荐指数
1
解决办法
355
查看次数

SurfaceView 与 Android 8.0 上的另一个 SurfaceView 重叠

我在 Android 8.0 上的应用程序遇到了一些奇怪的问题。我有自己的可滚动小部件,代码在 github可用。它有两个孩子,可以无限期地一一滚动。

在屏幕上,棋盘是一个SurfaceView,上面有棋子的可滚动移动子也是一个SurfaceView。可滚动视图的另一个子元素是简单的 LinearLayout。

问题是当子 SurfaceView 放置在棋盘下方时,它显示在其上方,而另一个子则正常滚动。看看下面的gif。

在此处输入图片说明

这个问题出现在 Android 8 上。在所有以前的版本上它都运行良好。

android surfaceview android-8.0-oreo android-8.1-oreo

1
推荐指数
1
解决办法
620
查看次数

错误:com.google.android.gms.internal.zzij中引用com.google.android.gms.common.internal.zzg作为接口。

更新Android Studio 3.2.1后,我尝试修复我的应用程序的某些功能。但似乎我得到更多的错误。当我尝试重建应用程序时。我得到了这个使者:

Error: Type com.google.android.gms.common.internal.zzg is referenced as an interface from `com.google.android.gms.internal.zzij`.
Run Code Online (Sandbox Code Playgroud)

但是,我的build.gradle(Module:app)和build.gradle(Project)看起来不错。这是我的build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 28

    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }

    buildToolsVersion '28.0.3'
    useLibrary 'org.apache.http.legacy'

    defaultConfig {
        applicationId "com.xx.xx"
        minSdkVersion 15
        targetSdkVersion 28
        multiDexEnabled = true
        versionCode 7x
        versionName "1.0.7x"
        testInstrumentationRunner 
    "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 
    'proguard-rules.pro'
        }
    }
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/NOTICE.txt'
    }
    configurations.all {
        resolutionStrategy.force 'com.google.code.findbugs:jsr305:3.0.1'
        exclude group: …
Run Code Online (Sandbox Code Playgroud)

java android android-studio

1
推荐指数
1
解决办法
3972
查看次数