1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Kỹ thuật lập trình >

Section 34.7: Clear your current Activity stack and launch a new Activity

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (11.9 MB, 1,329 trang )


Intent intent = new Intent(this, LoginActivity.class);

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK

|Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(intent);

finish();



GoalKicker.com – Android™ Notes for Professionals



245



Chapter 35: Activity Recognition

Activity recognition is the detection of a user's physical activity in order to perform certain actions on the device,

such as taking points when a drive is detected, turn wifi off when a phone is still, or putting the ring volume to max

when the user is walking.



Section 35.1: Google Play ActivityRecognitionAPI

This is a just a simple example of how to use GooglePlay Service's ActivityRecognitionApi. Although this is a great

library, it does not work on devices that do not have Google Play Services installed.

Docs for ActivityRecognition API

Manifest






android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/AppTheme">



















MainActivity.java

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,

GoogleApiClient.OnConnectionFailedListener {

private GoogleApiClient apiClient;

private LocalBroadcastManager localBroadcastManager;

private BroadcastReceiver localActivityReceiver;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

apiClient = new GoogleApiClient.Builder(this)

.addApi(ActivityRecognition.API)

.addConnectionCallbacks(this)

.addOnConnectionFailedListener(this)

.build();

//This just gets the activity intent from the ActivityReceiver class

localBroadcastManager = LocalBroadcastManager.getInstance(this);



GoalKicker.com – Android™ Notes for Professionals



246



localActivityReceiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

ActivityRecognitionResult recognitionResult =

ActivityRecognitionResult.extractResult(intent);

TextView textView = (TextView) findViewById(R.id.activityText);

//This is just to get the activity name. Use at your own risk.

textView.setText(DetectedActivity.zzkf(recognitionResult.getMostProbableActivity().getType()));

}

};

}

@Override

protected void onResume() {

super.onResume();

//Register local broadcast receiver

localBroadcastManager.registerReceiver(localActivityReceiver, new

IntentFilter("activity"));

//Connect google api client

apiClient.connect();

}

@Override

protected void onPause() {

super.onPause();

//Unregister for activity recognition

ActivityRecognition.ActivityRecognitionApi.removeActivityUpdates(apiClient,

PendingIntent.getBroadcast(this, 0, new Intent(this, ActivityReceiver.class),

PendingIntent.FLAG_UPDATE_CURRENT));

//Disconnects api client

apiClient.disconnect();

//Unregister local receiver

localBroadcastManager.unregisterReceiver(localActivityReceiver);

}

@Override

public void onConnected(@Nullable Bundle bundle) {

//Only register for activity recognition if google api client has connected

ActivityRecognition.ActivityRecognitionApi.requestActivityUpdates(apiClient, 0,

PendingIntent.getBroadcast(this, 0, new Intent(this, ActivityReceiver.class),

PendingIntent.FLAG_UPDATE_CURRENT));

}

@Override

public void onConnectionSuspended(int i) {

}

@Override

public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

}



ActivityReceiver

GoalKicker.com – Android™ Notes for Professionals



247



public class ActivityReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

LocalBroadcastManager.getInstance(context).sendBroadcast(intent.setAction("activity"));

}

}



Section 35.2: PathSense Activity Recognition

PathSense activity recognition is another good library for devices which don't have Google Play Services, as they

have built their own activity recognition model, but requires developers register at http://developer.pathsense.com

to get an API key and Client ID.

Manifest


android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/AppTheme">


















android:name="com.pathsense.android.sdk.CLIENT_ID"

android:value="YOUR_CLIENT_ID" />


android:name="com.pathsense.android.sdk.API_KEY"

android:value="YOUR_API_KEY" />





MainActivity.java

public class MainActivity extends AppCompatActivity {

private PathsenseLocationProviderApi pathsenseLocationProviderApi;

private LocalBroadcastManager localBroadcastManager;

private BroadcastReceiver localActivityReceiver;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

pathsenseLocationProviderApi = PathsenseLocationProviderApi.getInstance(this);

//This just gets the activity intent from the ActivityReceiver class

localBroadcastManager = LocalBroadcastManager.getInstance(this);



GoalKicker.com – Android™ Notes for Professionals



248



localActivityReceiver = new BroadcastReceiver() {

@Override

public void onReceive(Context context, Intent intent) {

//The detectedActivities object is passed as a serializable

PathsenseDetectedActivities detectedActivities = (PathsenseDetectedActivities)

intent.getSerializableExtra("ps");

TextView textView = (TextView) findViewById(R.id.activityText);

textView.setText(detectedActivities.getMostProbableActivity().getDetectedActivity().name());

}

};

}

@Override

protected void onResume() {

super.onResume();

//Register local broadcast receiver

localBroadcastManager.registerReceiver(localActivityReceiver, new

IntentFilter("activity"));

//This gives an update every time it receives one, even if it was the same as the last update

pathsenseLocationProviderApi.requestActivityUpdates(ActivityReceiver.class);

//

//



This gives updates only when it changes (ON_FOOT -> IN_VEHICLE for example)

pathsenseLocationProviderApi.requestActivityChanges(ActivityReceiver.class);

}

@Override

protected void onPause() {

super.onPause();

pathsenseLocationProviderApi.removeActivityUpdates();



//



pathsenseLocationProviderApi.removeActivityChanges();

//Unregister local receiver

localBroadcastManager.unregisterReceiver(localActivityReceiver);

}



}



ActivityReceiver.java

// You don't have to use their broadcastreceiver, but it's best to do so, and just pass the result

// as needed to another class.

public class ActivityReceiver extends PathsenseActivityRecognitionReceiver {

@Override

protected void onDetectedActivities(Context context, PathsenseDetectedActivities

pathsenseDetectedActivities) {

Intent intent = new Intent("activity").putExtra("ps", pathsenseDetectedActivities);

LocalBroadcastManager.getInstance(context).sendBroadcast(intent);

}

}



GoalKicker.com – Android™ Notes for Professionals



249



Chapter 36: Split Screen / Multi-Screen

Activities

Section 36.1: Split Screen introduced in Android Nougat

implemented

Set this attribute in your manifest's or element to enable or disable multi-window display:

android:resizeableActivity=["true" | "false"]



If this attribute is set to true, the activity can be launched in split-screen and freeform modes. If the attribute is set

to false, the activity does not support multi-window mode. If this value is false, and the user attempts to launch the

activity in multi-window mode, the activity takes over the full screen.

If your app targets API level 24, but you do not specify a value for this attribute, the attribute's value defaults to

true.

The following code shows how to specify an activity's default size and location, and its minimum size, when the

activity is displayed in freeform mode:

<--These are default values suggested by google.-->




android:defaultWidth="600dp"

android:gravity="top|end"

android:minHeight="450dp"

android:minWidth="300dp" />





Disabled features in multi-window mode

Certain features are disabled or ignored when a device is in multi-window mode, because they don’t make sense

for an activity which may be sharing the device screen with other activities or apps. Such features include:

1. Some System UI customization options are disabled; for example, apps cannot hide the status bar if they are

not running in full-screen mode.

2. The system ignores changes to the android:screenOrientation attribute.

If your app targets API level 23 or lower

If your app targets API level 23 or lower and the user attempts to use the app in multi-window mode, the system

forcibly resizes the app unless the app declares a fixed orientation.

If your app does not declare a fixed orientation, you should launch your app on a device running Android 7.0 or

higher and attempt to put the app in split-screen mode. Verify that the user experience is acceptable when the app

is forcibly resized.

If the app declares a fixed orientation, you should attempt to put the app in multi-window mode. Verify that when

you do so, the app remains in full-screen mode.



GoalKicker.com – Android™ Notes for Professionals



250



Chapter 37: Material Design

Material Design is a comprehensive guide for visual, motion, and interaction design across platforms and devices.



Section 37.1: Adding a Toolbar

A Toolbar is a generalization of ActionBar for use within application layouts. While an ActionBar is traditionally

part of an Activity's opaque window decor controlled by the framework, a Toolbar may be placed at any

arbitrary level of nesting within a view hierarchy. It can be added by performing the following steps:

1. Make sure the following dependency is added to your module's (e.g. app's) build.gradle file under

dependencies:

compile 'com.android.support:appcompat-v7:25.3.1'



2. Set the theme for your app to one that does not have an ActionBar. To do that, edit your styles.xml file

under res/values, and set a Theme.AppCompat theme.

In this example we are using Theme.AppCompat.NoActionBar as parent of your AppTheme:





You can also use Theme.AppCompat.Light.NoActionBar or Theme.AppCompat.DayNight.NoActionBar, or any

other theme that does not inherently have an ActionBar



3. Add the Toolbar to your activity layout:


android:id="@+id/toolbar"

android:layout_width="match_parent"

android:layout_height="?attr/actionBarSize"

android:background="?attr/colorPrimary"

android:elevation="4dp"/>



Below the Toolbar you can add the rest of your layout.



4. In your Activity, set the Toolbar as the ActionBar for this Activity. Provided that you're using the

appcompat library and an AppCompatActivity, you would use the setSupportActionBar() method:

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

//...



GoalKicker.com – Android™ Notes for Professionals



251



Xem Thêm
Tải bản đầy đủ (.pdf) (1,329 trang)

×