我有一个长时间运行的异步任务,它将一些数据发送到我的服务器,然后停止.整个过程可能涉及一些请求和响应.我必须从数据库中读取数据,发送它并处理响应并相应地更新我的数据库.我正在使用内容提供程序从数据库中读取和更新数据.
现在要使用Content Provider,我必须getContentResolver()在上下文中调用该方法.所以我想知道我是否必须使用getApplicationContext或只是传递Activity.this给我的方法.
我看了几个帖子是这样解释两个和大部分之间的区别,他们劝告我们不要使用getApplicationContext,如果可能的.虽然我不希望我AsyncTask失去从被破坏或方向改变Activity.this时的背景Activity.所以我想知道我是否可以使用getApplicationContext我的情况或使用Activity.this符合我的要求.
我正在使用Retrofit 2.0来进行返回Observables的api调用.当呼叫完成并且响应符合预期时,一切正常.现在让我们说我们有一个错误响应,它会抛出一个onError.我想阅读响应正文,即使它是一个错误.
例
@FormUrlEncoded
@POST("tokenLogin")
Observable<LoginResponse> loginWithToken(
@Field("token") String pin
);
Run Code Online (Sandbox Code Playgroud)
当请求和响应有效时,我得到正确的observable,并在出现错误时按预期调用onError.
正确回应:
{ "status" : "authenticated" }
Run Code Online (Sandbox Code Playgroud)
Observable将其转换为正确的Observable,我可以将响应读作LoginResponse对象.
现在,错误响应如下:
{ "errorMessage" : "You need to take some xyz action" }
Run Code Online (Sandbox Code Playgroud)
我想阅读该错误响应并将消息显示给用户.我该怎么做呢?
我的应用程序具有敏感的用户信息,我们需要实现一个密码屏幕,以便在用户打开应用程序时显示.以下是我在阅读本文后尝试的两种方法.
使用静态变量并将其重置onStop()为每个活动,并在每个活动中再次检查它,onStart()如果时间超过最小门限,则显示密码屏幕,例如1-2秒.这种方法的问题在于我的应用程序还使用意图调用相机和条形码扫描仪,用户可能会在这些外部应用程序中花费更长的时间.在这种情况下我可以增加阈值,但它使计算变得复杂并且不是一个非常好的解决方案.
我使用这种方法尝试了另一种方法.
protected boolean isAppOnForeground(final Context context) {
List<RunningAppProcessInfo> appProcesses = ((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)).getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = context.getPackageName();
for (RunningAppProcessInfo appProcess : appProcesses) {
if ((appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) &&
appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}
Run Code Online (Sandbox Code Playgroud)但是当我在每个活动的onStart方法中检查它时,这将始终返回true,因为该进程已经在onStart中启动
当用户打开应用程序时,我可以使用任何其他方法来显示密码吗?即使用户点击主屏幕退出应用程序然后从最近的应用程序返回应用程序,也应该显示它.
我试图从我们的服务器获取JSON响应,并且当字符串长度达到大约5525个字符时,响应字符串似乎总是被截断.
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
ResponseHandler<String> responseHandler= new BasicResponseHandler();
String testResponse = httpClient.execute(post, responseHandler);
Run Code Online (Sandbox Code Playgroud)
我还尝试使用HttpEntity并读取响应流.但是这也会在大约那个长度截断字符串.
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(URL);
// HttpGet get = new HttpGet(URL);
HttpResponse response = null;
HttpEntity entity = null;
InputStream inputStream = null;
BufferedReader reader = null;
String result = "";
try {
response = (HttpResponse)httpClient.execute(post);
entity = response.getEntity();
if(entity != null){
inputStream = entity.getContent();
}
reader = new BufferedReader(new InputStreamReader(inputStream), 8000);
StringBuffer builder …Run Code Online (Sandbox Code Playgroud) 我的应用程序中有一个语音识别部分来捕获用户的语音输入.
这就是我的工作
Intent voiceIntent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
startActivityForResult(voiceIntent, REQUEST_CODE);
Run Code Online (Sandbox Code Playgroud)
这在大多数设备上运行良好,但现在由于平板电脑越来越受欢迎,其中一些没有麦克风,它会引发错误
W/dalvikvm(408):threadid = 1:线程退出,未捕获异常(组= 0x40015560)E/AndroidRuntime(408):致命异常:主E/AndroidRuntime(408):android.content.ActivityNotFoundException:找不到要处理的活动Intent {act = android.speech.action.RECOGNIZE_SPEECH(有额外内容)} E/AndroidRuntime(408):在android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408).....
所以我想在让用户访问语音输入功能之前检测麦克风是否存在.如何检测设备上是否有麦克风.
谢谢.
我试图看看哪些实现更适合访问Android应用程序中的sqlite数据库
使用DatabaseHelper类扩展SqliteOpenHelper并使用单例模式.在某些罕见的情况下,我确实看到崩溃,因为数据库已关闭.虽然有点刺激,但我让他们通过只是因为它在宏伟的计划和我拥有的项目数量上是次要的.
public class DBHelper extends SQLiteOpenHelper {
private static DBHelper instance;
private final String CREATE_HEALTH_DATA_TABLE = "CREATE TABLE IF NOT EXISTS my_table ( "
+ BaseColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "
+ MT_FIELD_NAME + " TEXT NOT NULL );";
private DBHelper getInstance(Context c) {
if (instance == null) {
instance = new DBHelper(c);
}
return instance;
}
private DBHelper(Context c) {
super(c, "my_database.sqlite", null, 0);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
} …Run Code Online (Sandbox Code Playgroud) 什么线程是unsubscribeOn默认为我们没有指定它但仍指定subscribeOn的线程?我们是否需要指定我们想要取消订阅的线程,即使它与subscribeOn中使用的线程相同?
或者底部的两个片段是否为取消订阅做同样的事情?
选项1:
mSubscription.add(myAppProvider
.getSomeData()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.unsubscribeOn(Schedulers.io())
.subscribe(data -> handleData(data),
throwable -> handleError(throwable)
));
Run Code Online (Sandbox Code Playgroud)
选项2:
mSubscription.add(myAppProvider
.getSomeData()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(data -> handleData(data),
throwable -> handleError(throwable)
));
Run Code Online (Sandbox Code Playgroud)
我确实看过Rx-Java文档,但他们只解释了subscribeOn,但没有解释unSubscribeOn
我想检查用户是否在他/她的设备上启用了背景数据,并在禁用时显示消息.
如何检查是否已启用?我试过了
import android.provider.Settings;
//...
Settings.System.getString(getContentResolver(), Settings.Secure.BACKGROUND_DATA);
//and
Settings.Secure.getString(getContentResolver(), Settings.Secure.BACKGROUND_DATA);
Run Code Online (Sandbox Code Playgroud)
但他们正在返回null.
谢谢,成就.
我想使用设备的SD卡在设备上存储我的应用程序文件,图像,缓存文件等,并在我的应用程序中使用它们.我知道我可以使用Context.getFilesDir()/ Context.openFileOutput方法将它们保存在内部存储器中,但我不希望我的应用程序使用内部存储器来保存这些数据.
那么我有什么方法可以在SD卡上的目录中创建/保存我的所有应用程序特定文件,但是仍然可以让系统知道在卸载应用程序时必须删除这些文件吗?
或者在这种情况下有什么建议.
编辑:我也想支持2.1.
谢谢.