如果数据连接已打开,则显示一个对话框。否则没有

时间:2016-05-05 12:32:14

标签: android

如果数据连接打开,则显示一个对话框。我怎么创建。我是新开发者,所以请给。 MainActivity代码和xml视图代码。

#!/usr/bin/env ruby

File.write('woods.txt',
"The woods are lovely, dark, and deep
But I have promises to keep
And miles to go before I sleep
And miles to go before I sleep")

word_arrays = File.readlines('woods.txt').each_with_object([]) do |line, word_arrays|
  word_arrays << line.split
end

word_arrays.each.with_index do |words, index|
  puts "#{index}: #{words} "
end

=begin
Prints:

0: ["The", "woods", "are", "lovely,", "dark,", "and", "deep"]
1: ["But", "I", "have", "promises", "to", "keep"]
2: ["And", "miles", "to", "go", "before", "I", "sleep"]
3: ["And", "miles", "to", "go", "before", "I", "sleep"]
=end

1 个答案:

答案 0 :(得分:1)

以下是一种可用于验证数据连接是否可用的方法:

public static boolean isDataConnectionAvailable(Context context) {
        ConnectivityManager connectivityManager =
                (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
        return activeNetworkInfo != null && activeNetworkInfo.isConnectedOrConnecting();
}

如果返回true,则可以使用逻辑显示对话框。 StackOverflow上有很多可用于使用对话框的示例,因此我相信您可以找到这些并成功实现您的功能。