- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
很抱歉在http://stackoverflow.com搜索后提出这个问题。但在不同部分使用以下代码时,我无法处理下一个 Activity 。
这里,当用户未连接互联网时,将在alertdialog中弹出一个警告并显示“您未连接到互联网”,当用户单击alertdilog中的退出按钮时,应用程序将关闭。如果用户有互联网连接,则会弹出一个警告对话框,并在警报对话框中显示“您有互联网连接”,并且当用户单击继续按钮时,应用程序将继续执行下一个 Activity 。我在继续下一个 Activity 时遇到问题,该怎么办?
以下代码显示在其部分中
AndroidDetectInternetConnectionActivity.java中的代码
package com.example.detectinternetconnection;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class AndroidDetectInternetConnectionActivity extends Activity {
// flag for Internet connection status
Boolean isInternetPresent = false;
// Connection detector class
ConnectionDetector cd;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btnStatus = (Button) findViewById(R.id.btn_check);
// creating connection detector class instance
cd = new ConnectionDetector(getApplicationContext());
/**
* Check Internet status button click event
* */
btnStatus.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// get Internet status
isInternetPresent = cd.isConnectingToInternet();
// check for Internet status
if (isInternetPresent) {
// Internet Connection is Present
// make HTTP requests
AlertDialog.Builder builder = new AlertDialog.Builder(AndroidDetectInternetConnectionActivity.this);
builder.setMessage("You have network connection.")
.setTitle("Internet Connection")
.setCancelable(false);
AlertDialog alert = builder.create();
alert.setButton("Continue", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which){
// Do call some activity. Do what you wish to;
startActivity(new Intent(AndroidDetectInternetConnectionActivity.this,MainActivity2.class));
}
});
alert.show();
}
else {
// Internet connection is not present
// Ask user to connect to Internet
AlertDialog.Builder builder = new AlertDialog.Builder(AndroidDetectInternetConnectionActivity.this);
builder.setMessage("You need a network connection to use this application. Please turn on mobile network or Wi-Fi in Settings.")
.setTitle("No Internet Connection")
.setCancelable(false);
AlertDialog alert = builder.create();
alert.setButton2("Exit", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
//close the program
AndroidDetectInternetConnectionActivity.this.finish();
}
});
// Showing Alert Message
alert.show();
}
}
ConnectionDetector.java中的代码 包 com.example.Detectinternetconnection;
import android.app.AlertDialog;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
public class ConnectionDetector {
private Context _context;
public ConnectionDetector(Context context){
this._context = context;
}
public boolean isConnectingToInternet(){
ConnectivityManager connectivity = (ConnectivityManager) _context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null)
{
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED)
{
return true;
}
}
return false;
}
}
AndroidManifest.xml中的代码
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.detectinternetconnection"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".AndroidDetectInternetConnectionActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity2"></activity>
</application>
<!-- Internet Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
</manifest>
请告诉我哪里做错了?
最佳答案
总的代码很好,但最后只是通过create()创建初始化alertdialog..
if (isInternetPresent) {
// Internet Connection is Present
// make HTTP requests
AlertDialog.Builder builder = new AlertDialog.Builder(AndroidDetectInternetConnectionActivity.this);
builder.setMessage("You have network connection.")
.setTitle("Internet Connection")
.setCancelable(false);
alert.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which){
// Do call some activity. Do what you wish to;
startActivity(new Intent(AndroidDetectInternetConnectionActivity.this,MainActivity2.class));
}
});
AlertDialog alert = builder.create();
alert.show();
编辑
这是您的总 Activity 类别。
package com.example.detectinternetconnection;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class AndroidDetectInternetConnectionActivity extends Activity implements OnClickListener {
Boolean isInternetPresent = false;
ConnectionDetector cd;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Button btnStatus = (Button) findViewById(R.id.btnStatus);
cd = new ConnectionDetector(getApplicationContext());
btnStatus.setOnClickListener(this);
}
@Override
public void onClick(View v) {
isInternetPresent = cd.isConnectingToInternet();
if (isInternetPresent) {
AlertDialog alert;
AlertDialog.Builder builder = new AlertDialog.Builder(
AndroidDetectInternetConnectionActivity .this);
builder.setTitle("Internet Connection");
builder.setCancelable(false);
builder.setPositiveButton("Continue",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
startActivity(new Intent(AndroidDetectInternetConnectionActivity .this,
MainActivity2 .class));
}
});
alert = builder.create();
alert.show();
}
else {
AlertDialog alert;
AlertDialog.Builder builder = new AlertDialog.Builder(
AndroidDetectInternetConnectionActivity .this);
builder.setTitle("No Internet Connection");
builder.setMessage("Please Connect To Internet");
builder.setCancelable(false);
builder.setPositiveButton("Exit",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
alert = builder.create();
alert.show();
}
}
}
我想你会照顾其他类(class)。有关一些知识,你可以引用Android Dialog开发者网站上的教程。
关于java - 用户单击 AlertDialog OK 按钮后如何导航到下一个 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21546909/
我正在使用以下代码读取我在文本编辑器 (Notepad++) 中创建的文本 (.xml) 文件,将我从中读取的 UTF-8 文本转换为 UTF-16,以便 Windows API 函数可以使用它,然后
关闭。这个问题是opinion-based .它目前不接受答案。 想改进这个问题?更新问题,以便 editing this post 可以用事实和引用来回答它. 1年前关闭。 Improve this
我知道所有的论坛都充满了这样的问题,但我尝试了几个钩子(Hook),但它们不起作用(或者我做的不好)。 所以,我有: main.cpp <- fawn.h <- connector.cpp (defe
这是我正在使用的一段代码。 当 items 为 null 时,ok(Object items) 方法在内部调用 Jersey 的 Response.ok() 方法。 MembershipReq
我在 Tcl/Tk 中有一个简单的窗口,点击确定按钮运行模拟。我正在使用 Linux。模拟完成后,窗口将被销毁。问题是在模拟运行时窗口仍然存在。我希望窗口在我点击确定按钮后消失。 我尝试使用 wm w
在我们的网络应用程序中,我将 Angular-Materials $mdDialog 与确认对象一起使用。是否可以将按钮的顺序从取消-确定更改为确定-取消?并将初始焦点设置为取消按钮?也许通过 CSS
这个问题在这里已经有了答案: 关闭10年前。 Possible Duplicate: Ternary conditional operator in Python 我有这个问题,不知道要问谷歌: (v
我尝试使用 R 进行回归。我有以下代码,导入 CSV 文件没有问题 dat <- read.csv('http://pastebin.com/raw.php?i=EWsLjKNN',sep="
在 QInputDialog 中,如何去掉 OK 和 Cancel 按钮中的图标? 注意取消和确定的图标。我查看了属性按钮,不知道如何删除它们。 最佳答案 解决方案的策略是先获取按钮,但是这些属于QD
当使用Postman测试项目时,任何POST方法,我收到的是200 OK而不是201 Created,并且subreddit不是在数据库中创建的,并且在控制台休眠中接收到以下内容:SELECT T1_
当使用Postman测试项目时,任何POST方法,我收到的是200 OK而不是201 Created,并且subreddit不是在数据库中创建的,并且在控制台休眠中接收到以下内容:SELECT T1_
我制作了一个安卓应用程序,可以从本地 wifi 网络传输语音。为了收听和流式传输,我使用 JNI 中的 Opus C API 进行解码,并使用 OpenSL Audio 进行读取。 我从 Servic
我有一个定义如下的 map : mapMeasures := make(map[time.Time]models.Measure, 0) 与 type Measure struct { Del
这里我的数据集是 pd我已将其拆分为训练和测试数据 pd_train1和 pd_train2 sku national_inv lead_time in_transit_qty forecas
我已经检查过有关此问题的其他问题,但由于问题似乎非常具体,因此它们没有帮助。 我有一个像这样的数据框(这只是一个简单的示例,下面提供了来自 dput() 的示例数据): year species ab
当我使用 anova_test() 函数(来自 rstatix 包)做双向重复测量方差分析时,出现错误: lm.fit(x, y, offset = offset, singular.ok = sin
我一直在尝试对数据集进行 2-Way 重复测量测试,年份和疫苗类型是自变量,覆盖率是因变量。我用代码运行它: sat = anova_test( data=SA, dv = coverage, w
奇遇 我是一个普通的大学生,尹成是我的名字。顾名思义,我和其他人一样,没有什么特别之处。然而,在某个偶然的机会下,我发现了一个全新的领域——编程。 初印象 说实话,我对编程并不了解,甚至可以说是一窍不
我使用这些代码创建了一个卡拉 OK 并将其刻录到 VCD。 xxxxxx_1.m4a 文件是左声道(乐器),xxxxxx_0.m4a 文件是右声道(带人声的歌曲)。 将它们组合到 become xxx
我不确定为什么会发生这种情况,但我有一个简单的 Ajax 代码: $.ajax({ url: "/javascript/testing.js"}) .done(function(data){
我是一名优秀的程序员,十分优秀!