标签:
Handler handler = new Handler(Looper.getMainLooper());if (isExit) {handler.removeCallbacks(onBackTimeThread);isExit = false;finish();} else {isExit = true;Toast.makeText(this, "再按一下退出", Toast.LENGTH_SHORT).show();handler.postDelayed(onBackTimeThread, 3000);}
private String requestData(String urlString) {try {URL url = new URL(urlString);HttpURLConnection connection = (HttpURLConnection) url.openConnection();connection.setConnectTimeout(30000);connection.setRequestMethod("GET"); // GET POSTconnection.connect();int responseCode = connection.getResponseCode();//String responseMessage = connection.getResponseMessage();String result = null;if(responseCode == HttpURLConnection.HTTP_OK){InputStream inputStream = connection.getInputStream();Reader reader = new InputStreamReader(inputStream, "UTF-8");char[] buffer = new char[1024];reader.read(buffer);result = new String(buffer);} else {}return result;} catch (MalformedURLException e) {e.printStackTrace();Log.i("myLog","非法的uri");} catch (IOException e) {e.printStackTrace();}return null;}
public interface DataLoader<Result> {public Result loadData();;public void updateUi(Result result);}
public class SimpleAsyncTask<Result> extends AsyncTask<Void,Integer,Result>{private Context context;private ProgressDialog mProgressDialog;private String message;private DataLoader<Result> loader;public SimpleAsyncTask(Context context,int msgID,DataLoader<Result> loader){this.context=context;message=context.getString(msgID);this.loader=loader;}@Overrideprotected void onPreExecute() {super.onPreExecute();mProgressDialog=new ProgressDialog(context);mProgressDialog.setMessage(message);mProgressDialog.show();}@Overrideprotected Result doInBackground(Void... params) {try{return loader.loadData();}catch (Exception e){mProgressDialog.dismiss();}return null;}@Overrideprotected void onPostExecute(Result result) {loader.updateUi(result);mProgressDialog.dismiss();super.onPostExecute(result);}}
public class MusicService extends Service {public static final String TAG = MusicService.class.getSimpleName();private MediaPlayer mMediaPlayer;private MyBinder mMyBinder = new MyBinder();@Overridepublic void onCreate() {super.onCreate();mMediaPlayer = MediaPlayer.create(this, R.raw.chuxuezhe);//通知栏PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, ServiceTestActivity.class), 0);NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(this).setSmallIcon(R.mipmap.ic_launcher).setContentTitle("My notification").setContentText("Hello World!");mBuilder.setContentIntent(pendingIntent);NotificationManager mNotifyMgr =(NotificationManager) getSystemService(NOTIFICATION_SERVICE);mNotifyMgr.notify(1, mBuilder.build());}@Nullable@Overridepublic IBinder onBind(Intent intent) {mMediaPlayer.start();return mMyBinder;}@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {mMediaPlayer.start();return START_NOT_STICKY;}@Overridepublic boolean onUnbind(Intent intent) {return super.onUnbind(intent);}@Overridepublic void onDestroy() {super.onDestroy();mMediaPlayer.stop();stopForeground(true);}public class MyBinder extends Binder {public MusicService getService() {return MusicService.this;}}}
public class ServiceTestActivity extends AppCompatActivity implements View.OnClickListener{private Button start_service,stop_service;private boolean isBind = false ;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_service_test);start_service= (Button) findViewById(R.id.start_service);stop_service= (Button) findViewById(R.id.stop_service);start_service.setOnClickListener(this);stop_service.setOnClickListener(this);}private ServiceConnection mServiceConnection=new ServiceConnection() {private MusicService mMusicService;@Overridepublic void onServiceConnected(ComponentName componentName, IBinder iBinder) {MusicService.MyBinder binder= (MusicService.MyBinder) iBinder;mMusicService = binder.getService();}@Overridepublic void onServiceDisconnected(ComponentName componentName) {mMusicService=null;}};@Overridepublic void onClick(View view) {switch (view.getId()){case R.id.start_service:isBind =bindService(new Intent(ServiceTestActivity.this, MusicService.class),mServiceConnection,BIND_AUTO_CREATE);//startService(new Intent(ServiceTestActivity.this, MusicService.class));break;case R.id.stop_service:if (isBind) {unbindService(mServiceConnection);isBind = false;}//stopService(new Intent(ServiceTestActivity.this, MusicService.class));break;}}@Overrideprotected void onDestroy() {super.onDestroy();if (isBind) {unbindService(mServiceConnection);isBind = false;}}}
public class TestBroadcastReceiver extends BroadcastReceiver {public static final String TAG = "myLog";@Overridepublic void onReceive(Context context, Intent intent) {if(intent!=null){if(TextUtils.equals(intent.getAction(), SendBroadcastActivity.COM_XHB_ONLYSTAR)){Log.i(TAG, "onReceive:action "+intent.getAction());Log.i(TAG, "onReceive:broadcast "+intent.getStringExtra("toast"));}}}}
public class SendBroadcastActivity extends AppCompatActivity {public static final String COM_XHB_ONLYSTAR = "com.xhb.onlystar";private TestBroadcastReceiver mReceiver;private IntentFilter mFilter;private Button mSend;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_send_broadcast);//注册需要一个IntentFiltermFilter = new IntentFilter();mFilter.addAction(COM_XHB_ONLYSTAR);//注册需要一个广播接收器mReceiver = new TestBroadcastReceiver();mSend = (Button) findViewById(R.id.send);mSend.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {Intent intent=new Intent();intent.setAction(COM_XHB_ONLYSTAR);intent.putExtra("toast","this is the broadcast");sendBroadcast(intent);//发送多个广播能全部接收到//sendOrderedBroadcast(intent);//按照优先级接收// LocalBroadcastManager}});}@Overrideprotected void onStart() {super.onStart();//注册registerReceiver(mReceiver, mFilter);}@Overrideprotected void onStop() {super.onStop();//取消注册unregisterReceiver(mReceiver);}}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><FrameLayoutandroid:id="@+id/realtabcontent"android:layout_width="fill_parent"android:layout_height="0dip"android:layout_weight="1"android:background="@color/gray" /><android.support.v4.app.FragmentTabHostandroid:id="@android:id/tabhost"android:layout_width="fill_parent"android:layout_height="wrap_content"android:background="@color/white"/></LinearLayout>
public class TestTabHostActivity extends AppCompatActivity {private FragmentTabHost mTabhost;private List<Tab> mTabs = new ArrayList<Tab>(5);private LayoutInflater mInflater;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_test_tab_host);initView();}private void initView() {Tab tab_home = new Tab(HomeFragment.class, R.string.home, R.drawable.selector_icon_home);Tab tab_hot = new Tab(HomeFragment.class, R.string.hot, R.drawable.selector_icon_hot);Tab tab_category = new Tab(HomeFragment.class, R.string.catagory, R.drawable.selector_icon_category);Tab tab_cart = new Tab(HomeFragment.class, R.string.cart, R.drawable.selector_icon_cart);Tab tab_mine = new Tab(HomeFragment.class, R.string.mine, R.drawable.selector_icon_mine);mTabs.add(tab_home);mTabs.add(tab_hot);mTabs.add(tab_category);mTabs.add(tab_cart);mTabs.add(tab_mine);mInflater = LayoutInflater.from(this);mTabhost = (FragmentTabHost) findViewById(android.R.id.tabhost);mTabhost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);//必须要for (Tab tab : mTabs) {TabHost.TabSpec tabSpec = mTabhost.newTabSpec(getString(tab.getTitle()));tabSpec.setIndicator(buildIndicator(tab));mTabhost.addTab(tabSpec, tab.getFragment(), null);}mTabhost.getTabWidget().setShowDividers(LinearLayout.SHOW_DIVIDER_NONE);//取消item之间的分割线mTabhost.setCurrentTab(0);//默认选择第一个}//得到indicatorprivate View buildIndicator(Tab tab) {View view = mInflater.inflate(R.layout.tab_indicator, null);ImageView img = (ImageView) view.findViewById(R.id.icon_tab);TextView text = (TextView) view.findViewById(R.id.txt_indicator);img.setBackgroundResource(tab.getIcon());text.setText(tab.getTitle());return view;}}
标签:
原文地址:http://www.cnblogs.com/wisemen/p/5840664.html