小编Tho*_*Vos的帖子

android.os.FileUriExposedException:file:///storage/emulated/0/test.txt通过Intent.getData()暴露在app之外

当我尝试打开文件时,应用程序崩溃了.它可以在Android Nougat下运行,但在Android Nougat上它会崩溃.当我尝试从SD卡打开文件而不是从系统分区打开文件时,它只会崩溃.一些许可问题?

示例代码:

File file = new File("/storage/emulated/0/test.txt");
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "text/*");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent); // Crashes on this line
Run Code Online (Sandbox Code Playgroud)

日志:

android.os.FileUriExposedException:file:///storage/emulated/0/test.txt通过Intent.getData()暴露在app之外

编辑:

在定位Android Nougat时,file://不再允许使用URI.我们应该使用content://URI代替.但是,我的应用程序需要打开根目录中的文件.有任何想法吗?

android android-file android-7.0-nougat

682
推荐指数
15
解决办法
38万
查看次数

是否可以将ConstraintLayout放入ScrollView中?

所以最近,与Android Studio 2.2中有一个新的ConstraintLayout,使设计轻松了很多,但不像RelativeLayout并且Linearlayout,我不能使用ScrollView包围ConstraintLayot.这可能吗?如果是这样,怎么样?

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:layout_editor_absoluteX="0dp"
        tools:layout_editor_absoluteY="0dp">

        <android.support.constraint.ConstraintLayout
            android:id="@+id/constraintLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:layout_editor_absoluteX="0dp"
            tools:layout_editor_absoluteY="0dp">

            <!-- Have whatever children you want inside -->

        </android.support.constraint.ConstraintLayout>

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

user-interface android android-layout android-constraintlayout

80
推荐指数
8
解决办法
8万
查看次数

android - "导出的接收器不需要许可",用于从系统服务接收的接收器

我在AndroidManifest中声明了一些接收器:

<!-- no warning -->
<receiver
    android:name=".receivers.TriggerMonitoringBootReceiver"
    android:enabled="false">
    <intent-filter>
        <action android:name="android.intent.action.BOOT_COMPLETED" />
    </intent-filter>
</receiver>

<!-- no warning -->
<receiver
    android:name=".receivers.ScanResultsReceiver"
    android:enabled="false">
    <intent-filter>
        <action android:name="android.net.wifi.SCAN_RESULTS" />
    </intent-filter>
</receiver>

<!-- warning : Exported receiver does not require permission-->
<receiver
    android:name=".receivers.BatteryMonitoringReceiver"
    android:enabled="false">
    <intent-filter>
        <action android:name="@string/intent_action_setup_alarm" />
        <action android:name="@string/intent_action_cancel_alarm" />
        <action android:name="@string/intent_action_monitor" />
    </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

第一个是为了接受一个BOOT_COMPLETED动作.第二个是接收android.net.wifi.SCAN_RESULTS.第三个是接收我广播的一些动作(intent_action_monitor)和AlarmManager(intent_action_setup_alarm等)广播的一些动作.

两个问题:

  • 为什么我没有收到所有接收器的警告?
  • 我需要为接收器设置哪些权限才能从系统服务接收以更正警告(我理解它是什么,我不希望任何人使用我的接收器)? exported="false" 的启动接收器,无线接收器,报警接收等
    我想过使用自定义权限,android:protectionLevel="signatureOrSystem"但文档建议不要使用此保护级别自定义权限.那我该如何处理这个警告呢?

将非常感谢链接到文档和/或一些代码.

android broadcastreceiver android-manifest android-permissions android-broadcastreceiver

61
推荐指数
4
解决办法
4万
查看次数

如何实现像Android Lollipop应用程序的第一个启动教程:像Sheets,Slides应用程序?

我的Nexus 5中安装了Android 5.0最终版本.我注意到它在第一次启动时显示教程非常漂亮,干净和优雅."Sheets","Slides"等应用

我们如何在Android L兼容应用程序中实现这一点?

此外,该应用程序淡出第一个启动屏幕,然后显示教程.

教程屏幕1 初始屏幕淡出并显示教程

android android-5.0-lollipop

51
推荐指数
4
解决办法
4万
查看次数

如何分组3x3网格的单选按钮?

正如标题所描述的那样,我正在尝试将一个3x3单选按钮网格组合成一个单一的无线电组.在之前的一个问题中,我了解到,对于与单个组相对应的单选按钮,他们必须是他们将对应的无线电组的直接子项.当我试图在一个广播组中封装整个表格布局(在表行中使用单选按钮)时,我学到了很多.

跑进那堵墙,我尝试了以下几点:

<TableLayout android:id="@+id/table_radButtons" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content"
        android:layout_below="@+id/title_radGroup_buffer">

        <TableRow>
            <RadioGroup android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:orientation="horizontal"
                android:id="@+id/radGroup1">  

                <RadioButton android:id="@+id/rad1" 
                    android:text="Button1" 
                    android:layout_width="105px" 
                    android:layout_height="wrap_content" 
                    android:textSize="13px"></RadioButton>
                <RadioButton android:id="@+id/rad2" 
                    android:text="Button2" 
                    android:layout_width="105px" 
                    android:textSize="13px" 
                    android:layout_height="wrap_content"></RadioButton>
                <RadioButton android:id="@+id/rad3" 
                    android:text="Button3" 
                    android:layout_width="105px" 
                    android:textSize="13px" 
                    android:layout_height="wrap_content"></RadioButton>
            </RadioGroup>
        </TableRow>
        <TableRow>
            <RadioGroup android:layout_width="fill_parent" 
                android:layout_height="wrap_content" 
                android:orientation="horizontal"
                android:id="@+id/radGroup1">
                  <!-- snippet -->
        </TableRow>
        <!-- snippet --->
</TableLayout>
Run Code Online (Sandbox Code Playgroud)

显然我第一次没有学习,因为我再次遇到了一堵墙.我希望不同表行中的单选按钮会注意到它们是同一个无线电组的一部分(给每个组提供相同的ID),但这不起作用.

有没有什么方法可以将所有这些按钮分组到一个单独的无线电组中并仍然保持我的3x3结构(每行3行,3个单选按钮)?

java xml android

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

使用PendingIntent在app小部件中的Oreo上启动服务

我正在使用Android应用小部件.我正在创建一个PendingIntent对象并在方法中使用它RemoteViews#setOnClickPendingIntent().这是未决的意图:

// The below code is called in the onUpdate(Context, AppWidgetManager, int[]) method of the AppWidgetProvider class.

// Explicit intent
Intent intent = new Intent(context, MyService.class);
intent.setAction(MY_ACTION);
intent.putExtra(EXTRA_KEY, VALUE);

// Create the pending intent
PendingIntent pendingIntent = PendingIntent.getService(context, appWidgetId, intent, 0);

// 'views' is a RemoteViews object provided by onUpdate in the AppWidgetProvider class.
views.setOnClickPendingIntent(R.id.root_layout, pendingIntent);
Run Code Online (Sandbox Code Playgroud)

上面的代码在Android Oreo之前按预期工作.但是,在Android Oreo中,如果将应用程序从最近的位置移除,它将不再启动该服务.(不再有效).PendingIntent在Oreo的后台执行限制中不排除?

出于测试目的,我更换getServicegetForegroundServiceService仍未启动.这两种方法都在日志中显示以下消息:

W/ActivityManager: Background start not allowed: service Intent { …
Run Code Online (Sandbox Code Playgroud)

android android-widget android-service android-appwidget android-8.1-oreo

16
推荐指数
2
解决办法
4912
查看次数

如何使RecyclerView无法点击并将onClick事件传递给父视图?

是否可以使RecyclerView不可点击?我想要这个,因为我RecyclerView只是在一个可点击的内容中 显示了一些小图标CardView.因此,如果有人点击图标,它应该只是点击(和动画)父母CardView.

我尝试过以下方法:

  1. recyclerView.setClickable(false);
  2. recyclerView.setFocusable(false);
  3. 延伸RecyclerViewonTouchEvent(MotionEvent)返回false.
  4. 使用上面的方法3并itemView.setClickable(false);RecyclerView适配器中使用.这样做,点击发送给父母.但是,现在RecyclerView不再可滚动了.
  5. 设置clickable="false",focusable="false",focusableInTouchMode="false"虚增列表项XML.(见评论@Ibrahim)
  6. 打电话recyclerView#setLayoutFrozen(true)itemView.setClickable(false);.这有效,但与#4有同样的问题.

有任何想法如何禁用和传递RecyclerView到父视图的点击事件?请注意,RecyclerView仍然需要可滚动(水平).


编辑:
User @ c.dunlap建议将OnClick监听器设置为图标,并将点击"重定向"到父级的点击操作.这可以工作,但父视图上不会有点击动画.如果有人在RecyclerViewitemView 外部点击 - 但仍在(例如ItemDecoration填充)内部 - 未检测到点击.所以不幸的是,这不是一个解决方案.

java android android-layout android-recyclerview

15
推荐指数
2
解决办法
5341
查看次数

如何在MenuItem上设置长按一下监听器(在NavigationView上)?

如何设置长按一下监听器MenuItem

我试过这个答案,但这个方法对我来说并不存在.有解决方案吗

码:

Menu menu = navigationView.getMenu();
MenuItem menuItem = menu.findItem(R.id.menu_item);

// TODO set a long click listener on the menuItem.
menuItem.setOnLongClickListener(...); // Method does not exist, any other solutions?
Run Code Online (Sandbox Code Playgroud)

编辑:我不想设置自定义ActionView,我想要长整型监听器MenuItem,而不需要自定义视图.

android android-menu android-navigationview

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

在Android上使用java.io.file进行root访问

我正在尝试创建一个根文件浏览器,但是,我在访问根目录时遇到了一些问题.当我尝试访问/ data时,该文件夹为空.

当我这样做:

File file = new File("/data/");
Run Code Online (Sandbox Code Playgroud)

然后请求该目录中的项目,它是空的.(没有root访问权限).我知道如何使用a来执行简单的root命令Process,但是在没有root的情况下它将不起作用.所以我需要能在所有手机上运行的东西.

我想过使用File无根设备和lsroot设备的命令,但不知道这是否是最好的解决方案.我也可以使用ls或不使用root,但我想使用File.

有没有办法制作根文件浏览器,同时还支持非root用户手机?

谢谢

java android file root

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

RxJava合并去抖动而不是去抖动可观察者

我有两个可观察量:

Observable O(open):在textview中打开一些内容的文件

可观察的E(编辑):在textview中编辑的文件内容

我想去掉E observable,并将它与O observable合并.

obs = Observable.merge(E.debounce(2000, TimeUnit.MILLISECONDS) , O)
                .subscribe(content->System.out.println("new content: " + content))
Run Code Online (Sandbox Code Playgroud)

问题是如果E发出事件E1并且在O发出O1事件之后,我们有输出:

new content: O1
new content: E1 // this output is rebundant (cuz we already have newer content O1)
Run Code Online (Sandbox Code Playgroud)

这是一个正在发生的事情的图表:图

如何从去抖动的观察中摆脱这个过度的旧事件?

java rx-java reactivex

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