安卓Notification小总结

Manifest设置:

  • 通知点击跳转的Activity设置LaunchMode为SingleTop

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
    
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    
    <activity
        android:name=".DetailActivity"
        android:label="@string/title_activity_detail"
        android:launchMode="singleTop" >
    </activity>
    

Activity获取数据

  • 依靠 OnNewIntent()
public class DetailActivity extends Activity {

    private TextView tv;

    @Override   
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        initView();

        initData();
    }

    private void initView() {
        tv = (TextView) findViewById(R.id.tv_second);
    }

    private void initData() {

        Intent intent = getIntent();

        handleIntent(intent);
    }

    /**
     * 当前SingleTop模式的Activity在栈顶时,再次启动才会调用这个方法
     */

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        handleIntent(intent);
    }

    /**
     * 处理通知
     */
    private void handleIntent(Intent intent) {

        if (intent == null)
            return;

        // 接收到通知
        if ("action_notification".equals(intent.getAction())) {
            String message = intent.getStringExtra("extra");

            if (!TextUtils.isEmpty(message)) {
                tv.setText(message);
            }
        }
    }

}

Notification核心方法:

private static void showNotification(String msg, Context mContext) {

    nm = (NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);

    Builder builder = new Notification.Builder(mContext);

    builder
        .setSmallIcon(R.mipmap.cloud)     //位于状态栏的图标
        .setTicker(msg);                   // 状态栏展示内容
        .setContentInfo("ContentInfo")
        .setContentText(msg)               // 通知栏内容
        .setContentTitle("资讯")            // 通知栏标题
        .setAutoCancel(true);               // 设置点击完当前通知就取消

        // 自定义通知布局(布局不过于复杂,简单的TextView/ImageView即可,测试添加ScrollView 报出难以理解的异常):
        // 使用自定义布局的情况,靠RemoteViews来设置布局中的TextView/ImageView的文字/图片内容:

        RemoteViews remoteViews = new RemoteViews(mContext.getPackageName(), R.layout.notification);
        remoteViews.setTextViewText(R.id.tv_notification, msg);
        remoteViews.setImageViewResource(R.id.iv_notification, R.mipmap.cloud);

        // 而不是如下传统方式:
        // View view = View.inflate(mContext, R.layout.notification, null);
        // TextView tv = (TextView) view.findViewById(R.id.tv_notification);
        // tv.setText(msg);

        builder.setContent(remoteViews);

    // 打开多个Activity--->getActivities
    // 必须设置PendingIntent,提供给通知的点击事件

    // TODO 设置目的Activity
    Intent intent_destAct = new Intent(mContext, DetailActivity.class);
    intent_destAct.putExtra("extra", msg);
    intent_destAct.setAction("action_notification");

    // TODO 设置伴随打开的Activity,一般不会只跳转一个Activity,否则点击Back就尴尬了 
    //注意数组中第一个intent要设置flag,flag_New_task,否则每点击一次Notification就打开一次Activity(忽略LaunchMode!)

    Intent intent_addAct = new Intent(mContext, MainActivity.class);
    intent_addAct.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

    Intent[] intents = new Intent[] { intent_addAct, intent_destAct };

    // 如果只启动单个Activity使用getActivity:
    // PendingIntent pendingIntent = PendingIntent.getActivity(mContext, i++, intent_destAct, PendingIntent.FLAG_CANCEL_CURRENT);


    // 参数二 requestCode 每个通知需设置成不同的,否则对应Activity的onNewIntent()回调拿不到对应的intent
    PendingIntent pendingIntent = PendingIntent.getActivities(mContext, i++, intents,
            PendingIntent.FLAG_CANCEL_CURRENT);

    builder.setContentIntent(pendingIntent);

    Notification notification = builder.build();

    // 参数一:tag.不同的tag对应不同的notification,这样一个app可以弹出多个通知.
    nm.notify(i, notification);

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值