在应用程序的内部存储中重命名文件的最佳/最简单方法是什么?我发现有一种Context.deleteFile()方法有点奇怪,但没有"移动"或"重命名"功能.我是否必须一直保存文件内容,删除文件,创建新内容然后将内容复制到文件中?或者有没有办法在现有文件上复制文件?
更新(2012年8月30日):
根据下面建议的解决方案,我无法开展工作:
我试过这个:
ctx.deleteFile("shoppinglists.csv"); <--- delete the old file
File oldfile = new File("shoppinglists.tmp");
File newfile = new File("shoppinglists.csv");
oldfile.renameTo(newfile);
Run Code Online (Sandbox Code Playgroud)
但是,这不起作用.在deleteFile()之后,没有更多的事情发生了,我留下了新的shoppinglists.tmp文件.
我错过了什么?
注意:LogCat中没有错误或任何内容.
我的代码用于集成FCM ..我希望在推送到达时获取当前活动上下文.用于使用上下文转换侦听器的目的.
这里的代码片段代码......
public class MyFirebaseMessagingService extends FirebaseMessagingService {
private static final String TAG = "MyFMService";
private NotificationListener notificationListener;
private Context context;
private int count=0;
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
Log.d(TAG, "FCM Message Id: " + remoteMessage.getMessageId());
RemoteMessage.Notification notification = remoteMessage.getNotification();
Map<String, String> data = remoteMessage.getData();
Log.e("FROM", remoteMessage.getFrom());
count++;
//sendNotification(notification, data);
setNotificationCount();
}
private void setNotificationCount(AlertList alertList) {
notificationListener = (NotificationListener) context;
notificationListener.onNotificationMessage(count);
}
private void sendNotification(RemoteMessage.Notification notification, Map<String, String> data) {
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher);
Intent intent …Run Code Online (Sandbox Code Playgroud) service android interface android-context firebase-cloud-messaging
我正在为Android构建基于组件的应用程序.简而言之,我想在运行时加载一个APK文件并从中执行代码.
我DexClassLoader成功地使用加载和实例化一些类,问题在于阅读APK的资源.
我想创建一个自定义Context对象,我可以用它来加载这些资源(LayoutInflater例如一个实例),类似于createPackageContext(),这对我的应用程序不起作用,因为它只是查看已安装的软件包.
我们正在开发一个将在JVM和中使用的库Android.现在我们需要实施试用期通知.在其他平台(.Net和Mac)中显示弹出消息框就足够了.但到目前为止,我找不到为Android做这个的方法.
问题是要显示一些消息(toast或AlertDialog),我需要有一个实际的上下文.但是由于我们的库不包含UI元素并且不以任何方式与UI相关,因此我们不会要求用户在创建组件时传递上下文.
简而言之:
我有一个有两个活动的应用程序.
其中一个列出了几个值,而另一个则向DB添加了新值.
我有一个从SQLiteOpenHelper扩展的类,并管理数据库连接,查询等.
现在,我理解在SQLiteOpenHelper的构造函数中,您必须传递一个上下文,该上下文用于确定是否必须创建新数据库或打开现有数据库.
但是如果我在每个活动中都有一个SQLiteOpenHelper类的实例,那么上下文就会有所不同.
有办法避免这种情况吗?
谢谢.
我的应用程序中有几个类.有些是Activities,Services和Pure java类.我知道我可以在Activity中显示Toast消息,但我想从纯java类中显示Toast.
在java类中,我将上下文传递给构造函数,但这似乎没有显示toast.
我在Application类中创建了一个方法,它接受一个String作为参数,希望我可以使用Application上下文生成Toast,这里也没有欢乐.
如何从非服务或活动等的类生成Toast.
public class LoginValidate{
public LoginValidate(Context context) {
this.context = context;
nfcscannerapplication = (NfcScannerApplication) context
.getApplicationContext();
}
public void someMethod(){
nfcscannerapplication.showToastMessage(result);
}
}
Run Code Online (Sandbox Code Playgroud)
.
///然后在我的Application类中
public void showToastMessage(String message){
Toast.makeText(this.getApplictionContext(), "Encountered a problem with sending tag: " + message, Toast.LENGTH_LONG).show();
}
Run Code Online (Sandbox Code Playgroud) 你能否告诉我最好的做法是通过activity或context其他情况(;)
但...,
Activity要么ContextActivity全局(公共静态活动活动)是个好主意这是我的代码.你会改变什么?(基于良好的Android实践)
Context.getFilesDir() 对应的 XML 是什么?
我当前的 XML:
<paths xmlns:android="http://schemas.android.com/apk/res/android">
<files-path name="internal" path="." />
</paths>
Run Code Online (Sandbox Code Playgroud)
文件创建:
File video = new File(getFilesDir(), "movie.mp4");
videoUri = FileProvider.getUriForFile(this, "my.package.name.provider", video);
Run Code Online (Sandbox Code Playgroud) 我已经看到了一些使用Xamarin.Forms获取Android Context的旧解决方案.这些较旧的解决方案建议使用,Xamarin.Forms.Forms.Context但由于不断变化的Xamarin.Forms平台,这不再适用.
在我的示例中,我使用了DependencyService.Get<Interface>()来自我的共享代码项目来使用Interface该类的特定于平台的实现.
当使用Xamarin.Andriod使用特定于平台的功能时,需要使用Android Context.
那么我们如何获得Android Context的全局信息界面呢?
android android-context xamarin.android xamarin xamarin.forms
companion object {
@Volatile
private lateinit var instance: ExampleDatabase
fun getInstance(context: Context): ExampleDatabase {
synchronized(this) {
if(!::instance.isInitialized) {
instance = Room.databaseBuilder(
context.applicationContext, // Why does this require context?
LottoDatabase::class.java,
"lotto_database"
)
.fallbackToDestructiveMigration()
.build()
}
return instance
}
}
}
Run Code Online (Sandbox Code Playgroud)
上面的代码是创建房间数据库单例的一般方式。我想知道为什么 Room.databaseBuilder 函数需要上下文作为参数。我知道这个问题可能很愚蠢,因为我对 Android 中的上下文缺乏了解。
android android-context kotlin android-database android-room