gpt4 book ai didi

java - 无法解析符号 ConnectionCallbacks

转载 作者:行者123 更新时间:2023-12-01 11:08:38 35 4
gpt4 key购买 nike

我是安卓新手。我正在使用安卓工作室。我正在尝试构建一个可以将当前位置发送给我的 friend 的应用程序。我正在关注 https://developers.google.com/ ,但是当我尝试在主 Activity 中实现 ConnectionCallbacksOnConnectionFailedListener 时,它显示了一个错误,例如

:cannot resolve symbol ConnectionCallbacks

我尝试改变

public class MainActivity extends AppCompatActivity implements ConnectionCallbacks, OnConnectionFailedListener{ };

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

但没有帮助。

mainActivity.java

package com.example.manju.helpme;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationServices;

public class MainActivity extends AppCompatActivity implements ConnectionCallbacks, OnConnectionFailedListener{

/* called when user clicks ALERT! button*/


public void sendMessage(){

String phoneNo = "1234567890";
String message = "hi";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);

}

protected synchronized void buildGoogleApiClient(){
GoogleApiClient mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}

@Override
public void onConnected(Bundle connectionHint)
{
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if(mLastLocation != null){
mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
}
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnAlert = (Button)findViewById(R.id.btn_Alert);
Button btnAddGuardian = (Button)findViewById(R.id.btn_Add_Guardian);
Button btnRemoveGuardian= (Button)findViewById(R.id.btn_Remove_Guardian);



btnAlert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

sendMessage();
}
});

btnAddGuardian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});


btnRemoveGuardian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

gradle.build

apply plugin: 'com.android.application'

android {
compileSdkVersion 23
buildToolsVersion "23.0.0"

defaultConfig {
applicationId "com.example.manju.helpme"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:23.0.0'
compile 'com.google.android.gms:play-services:7.8.0'
}

androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.manju.helpme" >


<uses-permission android:name="android.permission.SEND_SMS"> </uses-permission>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >

<activity
android:name=".MainActivity"
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=".Guardians"
android:label="@string/title_activity_guardians"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.manju.helpme.MainActivity" />
</activity>
</application>

</manifest>

最佳答案

我们必须在 ConnectionCallbacks 中实现 OnConnectionSuspished 来消除此错误,直到出现此错误为止。

修改后的代码为:

mainActivity.java:

package com.example.manju.helpme;


import android.location.Location;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationServices;

import org.w3c.dom.Text;


public class MainActivity extends AppCompatActivity implements ConnectionCallbacks, OnConnectionFailedListener {

/* called when user clicks ALERT! button*/

protected Location mLastLocation;
protected TextView mLatitudeText;
protected TextView mLongitudeText;
protected GoogleApiClient mGoogleApiClient;
protected static final String TAG = "basic-location-sample";


public void sendMessage() {

String phoneNo = "8152873220";
String message = "hi";
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(phoneNo, null, message, null, null);

}

protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();

}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}

@Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}

/**
* Runs when a GoogleApiClient object successfully connects.
*/
@Override
public void onConnected(Bundle connectionHint) {
// Provides a simple way of getting a device's location and is well suited for
// applications that do not require a fine-grained location and that do not need location
// updates. Gets the best and most recent location currently available, which may be null
// in rare cases when a location is not available.
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
} else {
Toast.makeText(this,"No location detected, Make sure that location is enabled in the device", Toast.LENGTH_LONG).show();
}
}

@Override
public void onConnectionFailed(ConnectionResult result) {
// Refer to the javadoc for ConnectionResult to see what error codes might be returned in
// onConnectionFailed.
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
}


@Override
public void onConnectionSuspended(int cause) {
// The connection to Google Play services was lost for some reason. We call connect() to
// attempt to re-establish the connection.
Log.i(TAG, "Connection suspended");
mGoogleApiClient.connect();
}






@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

buildGoogleApiClient();


Button btnAlert = (Button)findViewById(R.id.btn_Alert);
Button btnAddGuardian = (Button)findViewById(R.id.btn_Add_Guardian);
Button btnRemoveGuardian= (Button)findViewById(R.id.btn_Remove_Guardian);



btnAlert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

sendMessage();
}
});

btnAddGuardian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});


btnRemoveGuardian.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

}
});

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

关于java - 无法解析符号 ConnectionCallbacks,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32633745/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com