Questions tagged «android»

Android是Google的移动操作系统,用于对数字设备(智能手机,平板电脑,汽车,电视,Wear,Glass和IoT)进行编程或开发。对于与Android相关的主题,请使用特定于Android的标签,例如android-intent,android-activity,android-adapter等。对于与开发或编程无关但与Android框架相关的问题,请使用以下链接:https:// android.stackexchange.com。

9
Android:context.getDrawable()的替代方法
我context.getDrawable()在项目中使用过这样的代码: Drawable greenProgressbar = context.getDrawable(R.drawable.custom_progressbargreen); 但是Eclipse给我一个错误,它需要一个Minimum API level of 21。这意味着经过Google的快速搜索后,我的APP只能在上使用Android 5.0。由于并非所有设备都在使用此版本的android,因此我希望有一个替代版本context.getDrawable()。

5
从程序包名称获取应用程序名称
我想从应用程序包名称中获取应用程序名称。有人请告诉我我怎么能得到这个。 public class AppInstalledListener extends BroadcastReceiver{ @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub String action = intent.getAction(); if(action.equals("android.intent.action.PACKAGE_ADDED")){ Logger.debug("DATA:"+intent.getData().toString()); } if(action.equals("android.intent.action.PACKAGE_REMOVED")){ Logger.debug("DATA:"+intent.getData().toString()); } if(action.equals("android.intent.action.PACKAGE_REPLACED")){ Logger.debug("DATA:"+intent.getData().toString()); } } }
84 android 

4
相当于Flutter中的RelativeLayout
有没有办法实现类似于RelativeLayoutAndroid上的功能? 特别是我在寻找类似的东西来centerInParent,layout_below:<layout_id>,layout_above:<layout_id>,和alignParentLeft 有关RelativeLayout的更多参考:https : //developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html 编辑:这是一个依赖于布局的示例 RelativeLayout 因此,在上图中的顶部,“豆腐的歌曲”文本与centerInParent内对齐RelativeLayout。而其他两个是alignParentLeft和alignParentRight 在每个带有火图标的单元格上,其底部的点赞次数围绕着火图标的中心对齐。同样,每个单元格的顶部和底部标题分别与图像头像的右侧和顶部和底部对齐。

5
DexException:无法将新索引65536合并为非巨型指令
由于某些未知原因,我的应用突然无法从Android Studio构建。 我不断 > com.android.ide.common.internal.LoggedErrorException: Failed to run command: /home/martynas/android-sdk/build-tools/19.1.0/dx --dex --num-threads=4 --output ... ... ... Error Code: 2 Output: UNEXPECTED TOP-LEVEL EXCEPTION: com.android.dex.DexException: Cannot merge new index 65536 into a non-jumbo instruction! 从命令行成功构建相同的应用程序。我检查了方法引用计数,它远低于可怕的64k。 我现在使用AS 0.8.11。


3
Android数据库交易
我已经创建了一个数据库。我想做交易。SaveCustomer()包含多个语句,以便Customer, CustomerControl, Profile, Payment在那时将记录插入表中。 当用户调用 SaveCustomer()method时,该数据将进入这4个表。那么我该如何进行交易?如果一个表插入失败,则需要回滚所有内容。例如,当第3个表插入记录时出现错误,那么还需要回滚前两个表的插入记录。 看我的代码: public void saveCustomer(){ DBAdapter dbAdapter = DBAdapter.getDBAdapterInstance(RetailerOrderKeyActivity.this); dbAdapter.openDataBase(); ContentValues initialValues = new ContentValues(); initialValues.put("CustomerName",customer.getName()); initialValues.put("Address",customer.getAddress()); initialValues.put("CustomerPID",strPID); initialValues.put("Date",strDateOnly); long n = dbAdapter.insertRecordsInDB("Customer", null, initialValues); } 同样也有其他陈述。 DBAdpter的代码是: public long insertRecordsInDB(String tableName, String nullColumnHack,ContentValues initialValues) { long n =-1; try { myDataBase.beginTransaction(); n = myDataBase.insert(tableName, …

5
在ExecutorService上调用shutdown()的原因
我读它颇有几分在过去的几个小时,我根本看不到任何理由(有效的理由)来调用shutdown()的ExecutorService,除非我们有一个巨大的应用程序,商店,几十个几十个不同的执行服务,不用于很长时间。 关闭(根据我的收集)唯一要做的事情是完成正常线程后所做的事情。当普通线程完成Runnable(或Callable)的run方法时,它将传递给Garbage Collection进行收集。使用Executor Service,线程将被简单地搁置,它们不会被选中以进行垃圾回收。为此,需要关机。 好,回到我的问题。有什么理由ExecutorService经常要求关机,甚至在提交某些任务后立即关机?我想抛弃有人正在做的情况,然后在awaitTermination()此之后立即调用,因为这已得到验证。一旦完成此操作,就必须重新创建一个新文件ExecutorService,以执行相同的操作。难道不是ExecutorService要重用线程的整个想法吗?那么,为什么要ExecutorService这么快就销毁呢? 简单地创建ExecutorService(或根据您需要的数量进行耦合),然后在应用程序运行期间将任务传给他们,然后在应用程序退出或其他重要阶段关闭这些执行程序,这是一种合理的方法吗? ? 我想从一些经验丰富的编码人员那里得到答案,他们确实使用ExecutorServices编写了许多异步代码。 第二个问题,与android平台的交易要小一些。如果你们中的某些人会说每次关闭执行程序都不是最好的主意,而您在android上编程,能否告诉我当我们处理不同的事件时如何处理这些关闭(具体来说-在执行时)应用程序生命周期。 基于CommonsWare的评论,我将该帖子设为中立。我真的对争论到死不感兴趣,而且看来它正在领先。我只想从经验丰富的开发人员那里了解到我在这里问的内容,如果他们愿意分享他们的经验。谢谢。

7
e.getMessage()和e.getLocalizedMessage()之间的区别
我在执行错误处理时使用这两种方法来获取catch块抛出的消息 他们俩都从错误处理中得到了我的消息,但是两者的区别到底是什么 我从互联网上搜索了一些内容,然后从这里想出了这个答案 Java异常从Throwable继承其getMessage和getLocalizedMessage方法(请参见相关链接)。区别在于子类应重写getLocalizedMessage以提供特定于语言环境的消息。例如,想象一下您正在将代码从一个说英语的公司/组改编为一个英语-英语组。您可能需要创建自定义Exception类,该类将覆盖getLocalizedMessage,以将拼写和语法正确化为将使用您的代码的用户和开发人员所期望的内容。这也可以用于异常消息的实际翻译。 问题:: 这意味着language specific实现吗?就像如果我使用e.getLocalizedMessage()我的应用程序English-错误将被抛出English,如果我使用我的应用程序Spanish-那么错误将被抛出Spanish 需要在何时何地可以使用这些方法的清楚说明
84 java  android 

9
您如何通过CURL向所有设备发送Firebase通知?
我正在尝试向所有应用程序用户发送通知(在Android上),实际上是在重复通过Firebase管理控制台发送通知时发生的情况。这是我开始的CURL命令: curl --insecure --header“ Authorization:key = AIzaSyBidmyauthkeyisfineL-6NcJxj-1JUvEM” --header“ Content-Type:application / json” -d“ {\” notification \“:{\” title \“:\” note -Title \“,\” body \“:\” note-Body \“}}”“ https://fcm.googleapis.com/fcm/send 这是解析出来的JSON,在您看来更容易: { "notification":{ "title":"note-Title", "body":"note-Body" } } 返回的响应只是两个字符: 至 就是这样,单词“ to”。(标题报告为400)我怀疑这与JSON中没有“ to”有关。人们甚至会为“到”做些什么?我没有定义主题,设备还没有注册任何东西。但是,他们仍然能够从Firebase管理面板接收通知。 由于Firebase通知处理的惊人局限性,我想尝试“仅数据” JSON包,即如果您的应用程序位于前台,则通知将由您的处理程序处理,但是如果您的应用程序位于后台,则它将得到由Firebase服务内部处理,并且从未传递给您的通知处理程序。显然,如果您通过API提交了通知请求,则可以解决此问题,但是仅当您仅使用数据时才可以解决。(这将破坏用同一条消息处理iOS和Android的能力。)在我的任何JSON中用“数据”替换“通知”均无效。 好的,然后我在这里尝试解决方案:Firebase Java Server将推送通知发送到所有设备 ,在我看来,这是“尽管可以通过管理控制台向所有人进行通知,但实际上不可能通过API进行通知。 ” 解决方法是让每个客户端都订阅一个主题,然后将通知推送到该主题。所以首先在onCreate中的代码: FirebaseMessaging.getInstance().subscribeToTopic("allDevices"); 然后我发送新的JSON: { "notification":{ "title":"note-Title", …

4
如何在Flutter项目中更改Android minSdkVersion
我正在尝试为使用蓝牙进行通信的应用程序启动一个Flutter项目。为此,我使用了颤动的蓝色。 不幸的是,当尝试运行(在Android设备上)我创建的第一个示例时,遇到以下错误: FAILURE: Build failed with an exception. * What went wrong: Execution failed for task ':app:processDebugManifest'. > Manifest merger failed : uses-sdk:minSdkVersion 16 cannot be smaller than version 19 declared in library [:flutter_blue] /home/maldus/Projects/flutter/polmac/build/flutter_blue/intermediates/manifests/full/debug/AndroidManifest.xml as the library might be using APIs not available in 16 Suggestion: use a compatible …

12
在Android Studio中运行参数化单元测试时,找不到给定的测试包含错误
我尝试在Android Studio中按以下方式运行参数化单元测试。 import android.test.suitebuilder.annotation.SmallTest; import junit.framework.TestCase; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameter; import org.junit.runners.Parameterized.Parameters; import java.util.Arrays; import java.util.Collection; @RunWith(Parameterized.class) @SmallTest public class FibonacciTest extends TestCase { @Parameters public static Collection<Object[]> data() { return Arrays.asList(new Object[][] { {0, 0}, {1, 1}, {2, 1}, {3, 2}, {4, 3}, …


12
Android将文件保存到外部存储
我在创建目录并将文件保存到我的android应用程序时遇到了一些问题。我正在使用这段代码来做到这一点: String filename = "MyApp/MediaTag/MediaTag-"+objectId+".png"; File file = new File(Environment.getExternalStorageDirectory(), filename); FileOutputStream fos; fos = new FileOutputStream(file); fos.write(mediaTagBuffer); fos.flush(); fos.close(); 但这引发了异常: java.io.FileNotFoundException:/mnt/sdcard/MyApp/MediaCard/MediaCard-0.png(无此类文件或目录) 在那条线上: fos = new FileOutputStream(file); 如果我将文件名设置为:"MyApp/MediaTag-"+objectId+"可以使用,但是如果我尝试创建文件并将其保存到另一个目录中,则会引发异常。所以有什么想法我做错了吗? 另一个问题是:有什么方法可以将我的文件设置为外部存储中的私有文件,以使用户仅在以如下方式连接设备时才能在图库中看到它们Disk Drive?

4
WebView方法在同一线程上出错
我有一个android程序(在webview中为Java + html)。我可以从javascript调用Java代码。但是另一种方式停止了工作(在Eclipse中更新之后)。 所以这就是我想要做的 制作网络视图(有效) 在JavaScript中调用AndroidFunction.test(); (工作) java test()函数调用webView.loadUrl(“ javascript:helloBack()”); (!不再工作了) 我试图让它与MainActivity中的WebView一起使用,但是没有起作用。 MainActivity.java public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); final WebView webView = (WebView)findViewById(R.id.webView); webView.getSettings().setJavaScriptEnabled(true); webView.setWebChromeClient(new WebChromeClient()); WebSettings webSettings = webView.getSettings(); webSettings.setJavaScriptEnabled(true); javascr = new Javascript(this, webView); webView.addJavascriptInterface(javascr, "AndroidFunction"); webView.loadUrl("file:///android_asset/www/index.html"); .... } Java脚本 public class Javascript { Context cont; …

12
Android和Facebook分享意图
我正在开发一个Android应用程序,并且想知道如何使用Android的共享意图从该应用程序内更新应用程序用户的状态。 浏览了Facebook的SDK后,看来这很容易做到,但是我渴望允许用户通过常规的“共享意图”弹出窗口来做到这一点吗?在这里看到: 我已经尝试了常用的共享意图代码,但是对于Facebook来说似乎不再起作用。 public void invokeShare(Activity activity, String quote, String credit) { Intent shareIntent = new Intent(android.content.Intent.ACTION_SEND); shareIntent.setType("text/plain"); shareIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, activity.getString(R.string.share_subject)); shareIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Example text"); activity.startActivity(Intent.createChooser(shareIntent, activity.getString(R.string.share_title))); } 更新:经过更多的挖掘,看来这是Facebook应用程序的一个错误,尚未解决!(facebook错误)同时,我似乎不得不忍受负面的“共享不起作用!!!” 评论。欢呼脸书:*(

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.