看黑马安卓基础教学视频总结(通知栏&反编译&Fragment)

本文详细介绍了安卓开发中的通知管理,包括如何发出和关闭通知,重点讲解了Fragment的使用,包括Fragment的概念、创建、添加到Activity、切换、生命周期以及与Activity的兼容性问题。此外,还探讨了Fragment之间的通信案例。

1.通知(★★★

通知用于在状态栏显示消息,消息到来时以图标方式表示,如果需要查看消息,可以拖动状态栏到屏幕下方即可查看消息,在Android中通过通知管理器NotificationManager来发出或关闭一个通知。

使用步骤:

1.    获取通知管理器对象

private NotificationManager manager;//通知管理器

//获取通知管理器服务

manager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);


2.    如何发出一个通知

public void notify(View view) {

//方式一:链式调用
/* Notification noti = new Notification.Builder(this)
        .setContentTitle("我是大标题")
        .setContentText("我是标题的内容")
        .setSmallIcon(R.drawable.ic_launcher)
        .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher))
        .build();

manager.notify(1,noti ) ;

*/

      //方式二: 创建一个通知

      Notificationnotification = new Notification(R.drawable.ic_launcher,// 消息的图标

            "您有一条来自黑马程序员的通知", // 消息的标题

            System.currentTimeMillis());// 消息发送的时刻立即发送

      // 定义一个隐式意图:指定点击通知时要打开的Activity

      Intentintent = newIntent();

      intent.setAction("com.itheima.noftify");

      /*

       * PendingIntent

       * 是延时意图,在未来某个时间开启一个界面,并且可以指定使用的次数,其实就是对Intent进行的一个包装,并且指定了使用次数

       */

PendingIntentpendingIntent = PendingIntent.getActivity(this, 100,intent, PendingIntent.FLAG_ONE_SHOT);

      //设置消息的内容和意图

      notification.setLatestEventInfo(this, "这是详细通知的标题", "这是通知的详细内容", pendingIntent);

      //设置通知点击后自动关闭

      notification.flags =Notification.FLAG_AUTO_CANCEL;

      //设置消息发送时开启灯光、声音、震动等特效,如果震动开启了需要设置权限

      notification.defaults =Notification.DEFAULT_ALL;

      //发送通知,给该通知的id为1

      manager.notify(1,notification);

   }


3.    如何关闭通知

 

// 关闭通知

   public voidclose(View view) {

      //关闭id为1的通知

      manager.cancel(1);

   }

       注意:

1.    若设置了弹出通知会有声音/震动/亮灯的效果,注意添加对应权限,否则会抛

错比如,设置震动需加权限android.permission.VIBRATE

              2.获取延期意图PendingIntent时,封装的意图对象必须采用隐式的方式



2.反编译(

Android打包好的APK如果直接解压缩,那么里面的布局文件和字节码文件如法直接使用,但是我们可以通过以下三个反编译工具对一个APK进行反编译。

1.apktool   反编译布局文件, 反编译之后会在apk相同的目录下创建一个

命令:apktool d xxx.apk

1.    dex2jar 把.dex文件转换成.jar的文件, 会在相同目录下生成一个

xxx.jar 

命令:dex2jar xxx.dex

       3.jd-gui        查看jar文件


3.Fragment★★★★

3.1.1什么是Fragment

       Fragment就是小型的Activity,它是在Android3.0时出现的。

       Fragment是表现Activity中UI的一个行为或者一部分。可以把fragment想象成activity的一个模块化区域,有它自己的生命周期,接收属于它自己的输入事件,并且可以在activity运行期间添加和删除(有点像一个可以在不同的activity中重用的“子Activity”)。

       Fragment必须被嵌入到一个activity中。它们的生命周期直接受其宿主activity的生命周期影响。当一个activity正在运行时,就可以独立地操作每一个Fragment,比如添加或删除它们。

       Fragment可以定义自己的布局、生命周期回调方法,因此可以将fragment重用到多个activity中,因此可以根据不同的屏幕尺寸或者使用场合改变fragment组合。

2.2如何创建一个Fragment

1.     为Fragment定义一个布局

2.     定义类继承Fragment

3.     重写类中的onCreateView方法,返回一个View对象作为当前Fragment的布局。

fragment第一次绘制它的用户界面的时候,系统会调用onCreateView()方法。为了绘制fragment的UI,此方法必须返回一个作为fragment布局的根的view。如果fragment不提供UI,可以返回null。

/**

 * 定义类继承Fragment

 */

public class TitleFragment extendsFragment {

   @Override

   public ViewonCreateView(LayoutInflater inflater, ViewGroup container, BundlesavedInstanceState) {

      //使用打气筒生成一个View对象

      Viewview = inflater.inflate(R.layout.fragment_title, null);

      return view;

   }

}


3.3如何将Fragment添加到Activity

       Activity必须在清单文件中进行声明,但是Fragment不需要,Fragment只需要在Activity的布局文件中声明就可以了。

<fragment

        android:id="@+id/fmt_title"

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

        android:name="com.itheima.fragment.TitleFragment"

        />


       注意:代码中的四个属性是必须的要给的,“android:name”属性:指定了在layout中实例化的Fragment类是哪个。

       当系统创建这个activity layout时,它实例化每一个在layout中指定的Fragment,并调用它们的onCreateView()方法,来获取每一个Fragment的layout,系统将从Fragment返回的View 直接插入到<fragment>元素所在的地方

       每一个fragment都需要一个唯一的标识,如果activity重启,系统可以用来恢复Fragment,并且可以用id来捕获Fragment来处理事务,例如移除它。

有3种方法来为一个fragment提供一个ID:

为android:id属性提供一个唯一ID;

为android:tag属性提供一个唯一字符串;

如果以上2个你都没有提供,系统将使用容器view的ID;

4.4如何切换Fragment

       要在activity中管理Fragment,需要使用FragmentManager可以通过调用activity的getFragmentManager()取得它的实例。

案例:点击不同的按钮切换到不同的Fragment进行显示

步骤:

1.     设置布局文件:添加三个按钮用于切换Fragment,并在按钮下方添加一个FrameLayout用来替换成响应的Fragment。

2.     创建三个Fragment,SportsFragment、NewsFragment、GameFragment。

public class SportsFragment extendsFragment {

   @Override

   public ViewonCreateView(LayoutInflater inflater, ViewGroup container, BundlesavedInstanceState) {

      // 使用打气筒生成一个View对象

Viewview = inflater.inflate(R.layout.fragment_sports, null);

      return view;

   }

}

其余两个Fragment跟SportsFragment代码一致,只是布局文件不同。


3.     添加切换Fragment的逻辑

public void news(View view){

      //获取Fragment管理器对象

      FragmentManagermanager = getFragmentManager();

      //开启事务

      FragmentTransactiontransaction = manager.beginTransaction();

      //将FrameLayout控件替换成Fragment对象

      transaction.replace(R.id.frame, newNewsFragment());

      //提交事务

      transaction.commit();

   }


sports()方法、games()方法同上,因此不再给出代码清单。


5.5 Fragment的生命周期

Fragment的生命周期和activity生命周期很像。

       onAttach:绑定到activity

       onCreate:创建fragment

       onCreateView: 创建fragment的布局

       onActivityCreated: activity创建完成后

       onStart: 可见, 不可交互

       onResume: 可见, 可交互

       onPause: 部分可见, 不可交互

       onStop:不可见

       onDestroyView: 销毁fragment的view对象

       onDestroy: fragment销毁了

       onDetach: 从activity解绑了

6.6 Fragment的向下兼容

Fragment是在Android 3.0才推出的,若想在3.0的低版本下使用Fragment,则需要执行下面2步:

1.     把所有Fragment和FragmentManager改成support-v4包下的类

2.     把Activity的继承改为FragmentActivity(support-v4包下的)

示例代码:

FragmentManager supportFragmentManager = getSupportFragmentManager();
FragmentTransaction beginTransaction = supportFragmentManager.beginTransaction(); //开启事物 

beginTransaction.replace(android.R.id.content, new Fragment1());


beginTransaction.replace(android.R.id.content, new Fragment2());


//[4]最后一步 记得commit
beginTransaction.commit();

7.7 Fragment之间的通信案例

       案例:创建一个用于显示选项卡的Fragment和一个用于显示内容的Fragment,当

选项卡切换时,使内容的Fragment信息跟着一起切换。

       步骤:

1.     添加内容区域Fragment的代码逻辑

public class ContentFragment extendsFragment {

   privateTextView tv_title;//标题

   privateImageView iv_pic;//内容

   @Override

   public ViewonCreateView(LayoutInflater inflater, ViewGroup container, BundlesavedInstanceState) {

      //使用打气筒填充布局

      Viewview = inflater.inflate(R.layout.fragment_content,null);

      //获取布局中的控件

      tv_title =(TextView) view.findViewById(R.id.tv_title);

      iv_pic =(ImageView) view.findViewById(R.id.iv_pic);

      return view;

   }

   //定义一个方法,用于改变标题和图片

   public voidsetTitleAndImage(String title,intpicId){

      tv_title.setText(title);

      iv_pic.setImageResource(picId);

   }

}


2.     添加选项卡区域Fragment的代码逻辑

public class TabFragment extends Fragment implements OnItemClickListener {

   privateString[] datas;

   private int[] picIds;

   @Override

   public ViewonCreateView(LayoutInflater inflater, ViewGroup container, BundlesavedInstanceState) {

      //直接使用ListView作为布局,因此不需要布局文件

      ListViewlistView = new ListView(getActivity());

      //定义一些常量数据作为册数数据

      datas = newString[]{"新闻","体育","财经","社会","娱乐","国际"};

      picIds = new int[]{R.drawable.a0,R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5};

      //设置点击事件

      listView.setOnItemClickListener(this);

      //声明一个ArrayAdapter对象

      ArrayAdapter<String>adapter = newArrayAdapter<String>(getActivity(), android.R.layout.simple_expandable_list_item_1, datas);

//给ListView设置Adapter

      listView.setAdapter(adapter);

      //返回视图

      return listView;

   }

   @Override

   public voidonItemClick(AdapterView<?> parent, View view, int position, long id) {

      //获取当前的title和图片id

      Stringtitle = datas[position];

      int picId= picIds[position];

      //获取Fragment管理器

      FragmentManagermanager = getFragmentManager();

      //通过ID选择出ContentFragment对象

      ContentFragmentfragment = (ContentFragment) manager.findFragmentById(R.id.fragment_content);

      //调用ContentFragment的方法,给ContentFragment传递参数,实现不同Fragment直接的通信

      fragment.setTitleAndImage(title,picId);

   }

}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值