小编Nik*_*sen的帖子

Android不会在文本文件中写新行

我正在尝试在android中的一个文本文件中写一个新行.

这是我的代码:

FileOutputStream fOut;
try {
    String newline = "\r\n";
    fOut = openFileOutput("cache.txt", MODE_WORLD_READABLE);
    OutputStreamWriter osw = new OutputStreamWriter(fOut); 

    osw.write(data);
    osw.write(newline);

    osw.flush();
    osw.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

我试过\n,\r\n我也尝试获取新行的系统属性,它们都不起作用.

数据变量包含来自同一文件的先前数据.

String data = "";

try {
    FileInputStream in = openFileInput("cache.txt");   
    StringBuffer inLine = new StringBuffer();
    InputStreamReader isr = new InputStreamReader(in, "ISO8859-1");
    BufferedReader inRd = new BufferedReader(isr,8 * 1024);
    String text;

    while ((text = inRd.readLine()) != null) {
        inLine.append(text); …
Run Code Online (Sandbox Code Playgroud)

java android android-file

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

Android Webview,缩放内容以适应屏幕

我的朋友使用javascript创建了一个应用程序,并将其上传到他的网站.

现在我试图将它包装到android中的webview中,并且在某些方面工作正常.

该页面是480x320

但无论我在Android上选择哪种屏幕,webview底部都有一个空白区域.我已经尝试了很多方法来缩放它,但没有任何效果.

我这时的代码是这样的

    final WebView browser = (WebView)findViewById(R.id.webview);   
    browser.getSettings().setJavaScriptEnabled(true);
    browser.loadUrl("http://page.xx");  
Run Code Online (Sandbox Code Playgroud)

java android webview

3
推荐指数
2
解决办法
2万
查看次数

删除服务中的持续通知

我有一个服务,它在启动时创建通知.

然后ondestroy()我希望它被删除.

我只是使用.cancel(NOTIFICATION_ID);

当它是正常通知时效果很好但是当我使用正在进行的事件时它不会取消它.

我确实读过一些关于如果android拥有资源的服务不会停止,但如何克服这一点?

我使用此代码启动服务

    final Intent bg_service = new Intent(BackgroundService.class.getName());

    btn_start = (Button)findViewById(R.id.btn_start);
    btn_stop = (Button)findViewById(R.id.btn_stop);

    btn_start.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            startService(bg_service);
        }
    });

    btn_stop.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            stopService(bg_service);
        }
    });
Run Code Online (Sandbox Code Playgroud)

我在创建中使用它

            notificationManager = (NotificationManager) 
            getSystemService(NOTIFICATION_SERVICE);

    Notification notification = new Notification(R.drawable.ic_launcher,
            "A new notification", System.currentTimeMillis());

    notification.flags |= Notification.FLAG_FOREGROUND_SERVICE;

    Intent intent = new Intent(this, mainapp.class);
    PendingIntent activity = PendingIntent.getActivity(this, 0, intent, 0);
    notification.setLatestEventInfo(this, "...",
            "...", activity);
    notificationManager.notify(19314, notification);
Run Code Online (Sandbox Code Playgroud)

这就是毁灭

            noficationManager.cancel(19314);
Run Code Online (Sandbox Code Playgroud)

java android android-service android-notifications

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