Java tutorial
/** * Copyright (C) 2016 Hyphenate Inc. All rights reserved. * * 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.vurtex.wakeup.activity; import android.annotation.SuppressLint; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.os.PowerManager; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentTransaction; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.TextView; import com.vurtex.wakeup.R; import com.vurtex.wakeup.fragment.Item1Fragment; @SuppressLint("NewApi") public class MainActivity extends FragmentActivity { protected static final String TAG = "MainActivity"; // textview for unread message count private TextView unreadLabel; // textview for unread event message private TextView unreadAddressLable; private Button[] mTabs; private Fragment contactListFragment; private Fragment[] fragments; private int index; private int currentTabIndex; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { String packageName = getPackageName(); PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE); if (!pm.isIgnoringBatteryOptimizations(packageName)) { Intent intent = new Intent(); intent.setAction(android.provider.Settings.ACTION_REQUEST_IGNORE_BATTERY_OPTIMIZATIONS); intent.setData(Uri.parse("package:" + packageName)); startActivity(intent); } } //make sure activity will not in background if user is logged into another device or removed setContentView(R.layout.activity_main); initView(); conversationListFragment = new Item1Fragment(); contactListFragment = new Item1Fragment(); Fragment settingFragment = new Item1Fragment(); fragments = new Fragment[] { conversationListFragment, contactListFragment, settingFragment }; getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, conversationListFragment) .add(R.id.fragment_container, contactListFragment).hide(contactListFragment) .show(conversationListFragment).commit(); //register broadcast receiver to receive the change } /** * init views */ private void initView() { unreadLabel = (TextView) findViewById(R.id.unread_msg_number); unreadAddressLable = (TextView) findViewById(R.id.unread_address_number); mTabs = new Button[3]; mTabs[0] = (Button) findViewById(R.id.btn_conversation); mTabs[1] = (Button) findViewById(R.id.btn_address_list); mTabs[2] = (Button) findViewById(R.id.btn_setting); // select first tab mTabs[0].setSelected(true); } /** * on tab clicked * * @param view */ public void onTabClicked(View view) { switch (view.getId()) { case R.id.btn_conversation: index = 0; break; case R.id.btn_address_list: index = 1; break; case R.id.btn_setting: index = 2; break; } if (currentTabIndex != index) { FragmentTransaction trx = getSupportFragmentManager().beginTransaction(); trx.hide(fragments[currentTabIndex]); if (!fragments[index].isAdded()) { trx.add(R.id.fragment_container, fragments[index]); } trx.show(fragments[index]).commit(); } mTabs[currentTabIndex].setSelected(false); // set current tab selected mTabs[index].setSelected(true); currentTabIndex = index; } @Override protected void onDestroy() { super.onDestroy(); if (exceptionBuilder != null) { exceptionBuilder.create().dismiss(); exceptionBuilder = null; } // try { // unregisterReceiver(**); // } catch (Exception e) { // } } /** * update unread message count */ public void updateUnreadLabel() { int count = getUnreadMsgCountTotal(); if (count > 0) { unreadLabel.setText(String.valueOf(count)); unreadLabel.setVisibility(View.VISIBLE); } else { unreadLabel.setVisibility(View.INVISIBLE); } } /** * update the total unread count */ public void updateUnreadAddressLable() { runOnUiThread(new Runnable() { public void run() { int count = getUnreadAddressCountTotal(); if (count > 0) { unreadAddressLable.setVisibility(View.VISIBLE); } else { unreadAddressLable.setVisibility(View.INVISIBLE); } } }); } /** * get unread event notification count, including application, accepted, etc * * @return */ public int getUnreadAddressCountTotal() { int unreadAddressCountTotal = 0; return unreadAddressCountTotal; } /** * get unread message count * * @return */ public int getUnreadMsgCountTotal() { int unreadMsgCountTotal = 0; int chatroomUnreadMsgCount = 0; return unreadMsgCountTotal - chatroomUnreadMsgCount; } @Override protected void onResume() { super.onResume(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK) { moveTaskToBack(false); return true; } return super.onKeyDown(keyCode, event); } private android.app.AlertDialog.Builder exceptionBuilder; private Fragment conversationListFragment; @Override protected void onNewIntent(Intent intent) { super.onNewIntent(intent); } }