Java 正在启动Android应用程序的活动
我目前正在开发一个Android应用程序,它集成了谷歌日历。正如我们所知,如果我们想选择一个活动作为开始活动,我们只需添加“意图过滤器”之类的东西。我在我的登录页活动中添加了,但当我在Emulator上调试时,它会在我的Google日历活动部分自动添加“意图过滤器”,并注释掉我在登录页添加的那个过滤器 这是AndroidManifest.xml中的代码Java 正在启动Android应用程序的活动,java,android,android-intent,Java,Android,Android Intent,我目前正在开发一个Android应用程序,它集成了谷歌日历。正如我们所知,如果我们想选择一个活动作为开始活动,我们只需添加“意图过滤器”之类的东西。我在我的登录页活动中添加了,但当我在Emulator上调试时,它会在我的Google日历活动部分自动添加“意图过滤器”,并注释掉我在登录页添加的那个过滤器 这是AndroidManifest.xml中的代码 <activity android:name="com.example.dylicious.mydocto
<activity
android:name="com.example.dylicious.mydoctors.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="com.example.dylicious.mydoctors.ViewDocActivity"
android:label="@string/title_activity_view_doc" >
</activity>
<activity
android:name="com.example.dylicious.mydoctors.ViewDoctorProf"
android:label="@string/title_activity_view_doctor_prof" >
</activity>
<activity
android:name="com.example.dylicious.mydoctors.StorePatientProfile"
android:label="@string/title_activity_store_patient_profile" >
</activity>
<activity
android:name="com.example.dylicious.mydoctors.ViewPatientProfile"
android:label="@string/title_activity_view_patient_profile" >
</activity>
<activity
android:name="com.example.dylicious.mydoctors.AppointmentActivity"
android:label="@string/title_activity_appointment" >
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.MAIN" />-->
<!--<category android:name="android.intent.category.LAUNCHER" />-->
<!--</intent-filter>-->
</activity>
这是我编辑的,AppointmentActivity是谷歌日历的一部分。我不停地评论,删除了AppointmentActivity和clean solution等中的部分,但没有任何效果。我只是想看看我是否遗漏了什么,因为我只是安卓应用开发领域的新手。提前多谢
而这是AppointmentActivity.java的类
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.api.client.extensions.android.http.AndroidHttp;
import com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.gson.GsonFactory;
import com.google.api.client.util.ExponentialBackOff;
import com.google.api.services.calendar.CalendarScopes;
import android.accounts.AccountManager;
import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.ScrollingMovementMethod;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.Arrays;
import java.util.List;
public class AppointmentActivity extends Activity {
com.google.api.services.calendar.Calendar mService;
GoogleAccountCredential credential;
private TextView mStatusText;
private TextView mResultsText;
ProgressDialog mProgress;
final HttpTransport transport = AndroidHttp.newCompatibleTransport();
final JsonFactory jsonFactory = GsonFactory.getDefaultInstance();
static final int REQUEST_ACCOUNT_PICKER = 1000;
static final int REQUEST_AUTHORIZATION = 1001;
static final int REQUEST_GOOGLE_PLAY_SERVICES = 1002;
private static final String PREF_ACCOUNT_NAME = "accountName";
private static final String[] SCOPES = { CalendarScopes.CALENDAR_READONLY };
/**
* Create the main activity.
* @param savedInstanceState previously saved instance data.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout activityLayout = new LinearLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
activityLayout.setLayoutParams(lp);
activityLayout.setOrientation(LinearLayout.VERTICAL);
activityLayout.setPadding(16, 16, 16, 16);
ViewGroup.LayoutParams tlp = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
mStatusText = new TextView(this);
mStatusText.setLayoutParams(tlp);
mStatusText.setTypeface(null, Typeface.BOLD);
mStatusText.setText("Retrieving data...");
activityLayout.addView(mStatusText);
mResultsText = new TextView(this);
mResultsText.setLayoutParams(tlp);
mResultsText.setPadding(16, 16, 16, 16);
mResultsText.setVerticalScrollBarEnabled(true);
mResultsText.setMovementMethod(new ScrollingMovementMethod());
activityLayout.addView(mResultsText);
mProgress = new ProgressDialog(this);
mProgress.setMessage("Calling Google Calendar API ...");
setContentView(activityLayout);
// Initialize credentials and service object.
SharedPreferences settings = getPreferences(Context.MODE_PRIVATE);
credential = GoogleAccountCredential.usingOAuth2(
getApplicationContext(), Arrays.asList(SCOPES))
.setBackOff(new ExponentialBackOff())
.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME, null));
mService = new com.google.api.services.calendar.Calendar.Builder(
transport, jsonFactory, credential)
.setApplicationName("Google Calendar API Android Quickstart")
.build();
}
/**
* Called whenever this activity is pushed to the foreground, such as after
* a call to onCreate().
*/
@Override
protected void onResume() {
super.onResume();
if (isGooglePlayServicesAvailable()) {
refreshResults();
} else {
mStatusText.setText("Google Play Services required: " +
"after installing, close and relaunch this app.");
}
}
/**
* Called when an activity launched here (specifically, AccountPicker
* and authorization) exits, giving you the requestCode you started it with,
* the resultCode it returned, and any additional data from it.
* @param requestCode code indicating which activity result is incoming.
* @param resultCode code indicating the result of the incoming
* activity result.
* @param data Intent (containing result data) returned by incoming
* activity result.
*/
@Override
protected void onActivityResult(
int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch(requestCode) {
case REQUEST_GOOGLE_PLAY_SERVICES:
if (resultCode != RESULT_OK) {
isGooglePlayServicesAvailable();
}
break;
case REQUEST_ACCOUNT_PICKER:
if (resultCode == RESULT_OK && data != null &&
data.getExtras() != null) {
String accountName =
data.getStringExtra(AccountManager.KEY_ACCOUNT_NAME);
if (accountName != null) {
credential.setSelectedAccountName(accountName);
SharedPreferences settings =
getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = settings.edit();
editor.putString(PREF_ACCOUNT_NAME, accountName);
editor.commit();
}
} else if (resultCode == RESULT_CANCELED) {
mStatusText.setText("Account unspecified.");
}
break;
case REQUEST_AUTHORIZATION:
if (resultCode != RESULT_OK) {
chooseAccount();
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
/**
* Attempt to get a set of data from the Google Calendar API to display. If the
* email address isn't known yet, then call chooseAccount() method so the
* user can pick an account.
*/
private void refreshResults() {
if (credential.getSelectedAccountName() == null) {
chooseAccount();
} else {
if (isDeviceOnline()) {
mProgress.show();
new ApiAsyncTask(this).execute();
} else {
mStatusText.setText("No network connection available.");
}
}
}
/**
* Clear any existing Google Calendar API data from the TextView and update
* the header message; called from background threads and async tasks
* that need to update the UI (in the UI thread).
*/
public void clearResultsText() {
runOnUiThread(new Runnable() {
@Override
public void run() {
mStatusText.setText("Retrieving data…");
mResultsText.setText("");
}
});
}
/**
* Fill the data TextView with the given List of Strings; called from
* background threads and async tasks that need to update the UI (in the
* UI thread).
* @param dataStrings a List of Strings to populate the main TextView with.
*/
public void updateResultsText(final List<String> dataStrings) {
runOnUiThread(new Runnable() {
@Override
public void run() {
if (dataStrings == null) {
mStatusText.setText("Error retrieving data!");
} else if (dataStrings.size() == 0) {
mStatusText.setText("No data found.");
} else {
mStatusText.setText("Data retrieved using" +
" the Google Calendar API:");
mResultsText.setText(TextUtils.join("\n\n", dataStrings));
}
}
});
}
/**
* Show a status message in the list header TextView; called from background
* threads and async tasks that need to update the UI (in the UI thread).
* @param message a String to display in the UI header TextView.
*/
public void updateStatus(final String message) {
runOnUiThread(new Runnable() {
@Override
public void run() {
mStatusText.setText(message);
}
});
}
/**
* Starts an activity in Google Play Services so the user can pick an
* account.
*/
private void chooseAccount() {
startActivityForResult(
credential.newChooseAccountIntent(), REQUEST_ACCOUNT_PICKER);
}
/**
* Checks whether the device currently has a network connection.
* @return true if the device has a network connection, false otherwise.
*/
private boolean isDeviceOnline() {
ConnectivityManager connMgr =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
return (networkInfo != null && networkInfo.isConnected());
}
/**
* Check that Google Play services APK is installed and up to date. Will
* launch an error dialog for the user to update Google Play Services if
* possible.
* @return true if Google Play Services is available and up to
* date on this device; false otherwise.
*/
private boolean isGooglePlayServicesAvailable() {
final int connectionStatusCode =
GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (GooglePlayServicesUtil.isUserRecoverableError(connectionStatusCode)) {
showGooglePlayServicesAvailabilityErrorDialog(connectionStatusCode);
return false;
} else if (connectionStatusCode != ConnectionResult.SUCCESS ) {
return false;
}
return true;
}
/**
* Display an error dialog showing that Google Play Services is missing
* or out of date.
* @param connectionStatusCode code describing the presence (or lack of)
* Google Play Services on this device.
*/
void showGooglePlayServicesAvailabilityErrorDialog(
final int connectionStatusCode) {
runOnUiThread(new Runnable() {
@Override
public void run() {
Dialog dialog = GooglePlayServicesUtil.getErrorDialog(
connectionStatusCode,
AppointmentActivity.this,
REQUEST_GOOGLE_PLAY_SERVICES);
dialog.show();
}
});
}
import com.google.android.gms.common.ConnectionResult;
导入com.google.android.gms.common.GooglePlayServicesUtil;
导入com.google.api.client.extensions.android.http.AndroidHttp;
导入com.google.api.client.googleapis.extensions.android.gms.auth.GoogleAccountCredential;
导入com.google.api.client.http.HttpTransport;
导入com.google.api.client.json.JsonFactory;
导入com.google.api.client.json.gson.GsonFactory;
导入com.google.api.client.util.ExponentialBackOff;
导入com.google.api.services.calendar.CalendarScopes;
导入android.accounts.AccountManager;
导入android.app.Activity;
导入android.app.Dialog;
导入android.app.ProgressDialog;
导入android.content.Context;
导入android.content.Intent;
导入android.content.SharedReferences;
导入android.graphics.Typeface;
导入android.net.ConnectivityManager;
导入android.net.NetworkInfo;
导入android.os.Bundle;
导入android.text.TextUtils;
导入android.text.method.ScrollingMovementMethod;
导入android.view.ViewGroup;
导入android.widget.LinearLayout;
导入android.widget.TextView;
导入java.util.array;
导入java.util.List;
公共类任命活动扩展活动{
com.google.api.services.calendar.calendar mService;
谷歌会计凭证;
私有文本视图mStatusText;
私有文本视图mResultsText;
进步;
final HttpTransport transport=AndroidHttp.newCompatibleTransport();
final JsonFactory JsonFactory=GsonFactory.getDefaultInstance();
静态最终整数请求\帐户\选择器=1000;
静态最终int请求_授权=1001;
静态最终整数请求\谷歌\播放\服务=1002;
私有静态最终字符串PREF\u ACCOUNT\u NAME=“accountName”;
私有静态最终字符串[]SCOPES={CalendarScopes.CALENDAR\u READONLY};
/**
*创建主活动。
*@param savedInstanceState以前保存的实例数据。
*/
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
LinearLayout activityLayout=新的LinearLayout(此);
LinearLayout.LayoutParams lp=新的LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_父级,
LinearLayout.LayoutParams.MATCH_PARENT);
activityLayout.setLayoutParams(lp);
活动布局。设置方向(线性布局。垂直);
activityLayout.setPadding(16,16,16,16);
ViewGroup.LayoutParams tlp=新建ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_内容,
ViewGroup.LayoutParams.WRAP_内容);
mStatusText=新文本视图(此);
mStatusText.setLayoutParams(tlp);
mStatusText.setTypeface(null,Typeface.BOLD);
setText(“检索数据…”);
activityLayout.addView(mStatusText);
mResultsText=新文本视图(此);
mResultsText.setLayoutParams(tlp);
mResultsText.setPadding(16,16,16,16);
mResultsText.setVerticalScrollBarEnabled(true);
mResultsText.setMovementMethod(新的ScrollingMovementMethod());
activityLayout.addView(mResultsText);
mpprogress=新建进度对话框(此对话框);
setMessage(“调用谷歌日历API…”);
setContentView(活动布局);
//初始化凭据和服务对象。
SharedReferences设置=getPreferences(Context.MODE\u PRIVATE);
凭证=GoogleAccountCredential.UsingAuth2(
getApplicationContext(),Arrays.asList(作用域))
.setBackOff(新的指数backoff())
.setSelectedAccountName(settings.getString(PREF_ACCOUNT_NAME,null));
mService=new com.google.api.services.calendar.calendar.Builder(
运输、jsonFactory、凭证)
.setApplicationName(“谷歌日历API Android快速启动”)
.build();
}
/**
*每当此活动被推送到前台时调用,例如
*对onCreate()的调用。
*/
@凌驾
受保护的void onResume(){
super.onResume();
如果(isGooglePlayServicesAvailable()){
刷新结果();
}否则{
mStatusText.setText(“需要谷歌播放服务:”+
“安装后,关闭并重新启动此应用。”);
}
}
/**
*在此处启动活动(特别是AccountPicker)时调用
*和授权)退出,并提供启动时使用的请求代码,
*它返回的结果代码,以及其中的任何附加数据。
*@param requestCode表示传入哪个活动结果的代码。
*@param resultCode代码,指示传入
*活动结果。
*@param data Intent(包含结果数据)由传入数据返回
*活动结果。
*/
@凌驾
活动结果上的受保护无效(
int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
开关(请求代码){
案例请求\谷歌\播放\服务: