/*
* Copyright (C) 2012 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.threadsample;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentManager.OnBackStackChangedListener;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
/**
* This activity displays Picasa's current featured images. It uses a service running
* a background thread to download Picasa's "featured image" RSS feed.
* <p>
* An IntentHandler is used to communicate between the active Fragment and this
* activity. This pattern simulates some of the communication used between
* activities, and allows this activity to make choices of how to manage the
* fragments.
*/
public class DisplayActivity extends FragmentActivity implements OnBackStackChangedListener {
// A handle to the main screen view
View mMainView;
// An instance of the status broadcast receiver
DownloadStateReceiver mDownloadStateReceiver;
// Tracks whether Fragments are displaying side-by-side
boolean mSideBySide;
// Tracks whether navigation should be hidden
boolean mHideNavigation;
// Tracks whether the app is in full-screen mode
boolean mFullScreen;
// Tracks the number of Fragments on the back stack
int mPreviousStackCount;
// Instantiates a new broadcast receiver for handling Fragment state
private FragmentDisplayer mFragmentDisplayer = new FragmentDisplayer();
// Sets a tag to use in logging
private static final String CLASS_TAG = "DisplayActivity";
/**
* Sets full screen mode on the device, by setting parameters in the current
* window and View
* @param fullscreen
*/
public void setFullScreen(boolean fullscreen) {
// If full screen is set, sets the fullscreen flag in the Window manager
getWindow().setFlags(
fullscreen ? WindowManager.LayoutParams.FLAG_FULLSCREEN : 0,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Sets the global fullscreen flag to the current setting
mFullScreen = fullscreen;
// If the platform version is Android 3.0 (Honeycomb) or above
if (Build.VERSION.SDK_INT >= 11) {
// Sets the View to be "low profile". Status and navigation bar icons will be dimmed
int flag = fullscreen ? View.SYSTEM_UI_FLAG_LOW_PROFILE : 0;
// If the platform version is Android 4.0 (ICS) or above
if (Build.VERSION.SDK_INT >= 14 && fullscreen) {
// Hides all of the navigation icons
flag |= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION;
}
// Applies the settings to the screen View
mMainView.setSystemUiVisibility(flag);
// If the user requests a full-screen view, hides the Action Bar.
if ( fullscreen ) {
this.getActionBar().hide();
} else {
this.getActionBar().show();
}
}
}
/*
* A callback invoked when the task's back stack changes. This allows the app to
* move to the previous state of the Fragment being displayed.
*
*/
@Override
public void onBackStackChanged() {
// Gets the previous global stack count
int previousStackCount = mPreviousStackCount;
// Gets a FragmentManager instance
FragmentManager localFragmentManager = getSupportFragmentManager();
// Sets the current back stack count
int currentStackCount = localFragmentManager.getBackStackEntryCount();
// Re-sets the global stack count to be the current count
mPreviousStackCount = currentStackCount;
/*
* If the current stack count is less than the previous, something was popped off the stack
* probably because the user clicked Back.
*/
boolean popping = currentStackCount < previousStackCount;
Log.d(CLASS_TAG, "backstackchanged: popping = " + popping);
// When going backwards in the back stack, turns off full screen mode.
if (popping) {
setFullScreen(false);
}
}
/*
* This callback is invoked by the system when the Activity is being killed
* It saves the full screen status, so it can be restored when the Activity is restored
*
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(Constants.EXTENDED_FULLSCREEN, mFullScreen);
super.onSaveInstanceState(outState);
}
/*
* This callback is invoked when the Activity is first created. It sets up the Activity's
* window and initializes the Fragments associated with the Activity
*/
@Override
public void onCreate(Bundle stateBundle) {
// Sets fullscreen-related flags for the display
getWindow().setFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR);
// Calls the super method (required)
super.onCreate(stateBundle);
// Inflates the main View, which will be the host View for the fragments
mMainView = getLayoutInflater().inflate(R.layout.fragmenthost, null);
// Sets the content view for the Activity
setContentView(mMainView);
/*
* Creates an intent filter for DownloadStateReceiver that intercepts broadcast Intents
*/
// The filter's action is BROADCAST_ACTION
IntentFilter statusIntentFilter = new IntentFilter(
Constants.BROADCAST_ACTION);
// Sets the filter's category to DEFAULT
statusIntentFilter.addCategory(Intent.CATEGORY_DEFAULT);
// Instantiates a new DownloadStateReceiver
mDownloadStateReceiver = new DownloadStateReceiver();
// Registers the DownloadStateReceiver and its intent filters
LocalBroadcastManager.getInstance(this).registerReceiver(
mDownloadStateReceiver,
statusIntentFilter);
/*
* Creates intent filters for the FragmentDisplayer
*/
// One filter is for the action ACTION_VIEW_IMAGE
IntentFilter displayerIntentFilter = new IntentFilter(
Constants.ACTION_VIEW_IMAGE);
// Adds a data filter for the HTTP scheme
displayerIntentFilter.addDataScheme("http");
// Registers the receiver
LocalBroadcastManager.getInstance(this).registerReceiver(
mFragmentDisplayer,
displayerIntentFilter);
// Creates a second filter for ACTION_ZOOM_IMAGE
displayerI
没有合适的资源?快使用搜索试试~ 我知道了~
android developer官网例子threadSample.zip

共44个文件
xml:22个
java:15个
png:4个


android developer->training->sending operations to multiple threads 的例子。demo的例子叫threadSample.zip。这个例子请结合官网文档看,对android多线程处理给出了一个解决方案,写的相当好。
资源推荐
资源详情
资源评论













收起资源包目录






























































共 44 条
- 1
资源评论

- lion5332018-09-25感谢楼主分享,急需的资源
- MapHacker_2015-06-24google的官方代码,很不错的多线程示例,看起来还是蛮吃力的。
- clanguagechushu102015-04-17官方代码,值得借鉴
- 仁凰2015-03-06终于找到了,非常感谢LZ
chenqidou
- 粉丝: 4
- 资源: 15

上传资源 快速赚钱
我的内容管理 收起
我的资源 快来上传第一个资源
我的收益
登录查看自己的收益我的积分 登录查看自己的积分
我的C币 登录后查看C币余额
我的收藏
我的下载
下载帮助

会员权益专享
安全验证
文档复制为VIP权益,开通VIP直接复制
