statusbar—NOTIFICATION

来源:互联网 发布:模拟退火算法模型 编辑:程序博客网 时间:2024/06/10 04:02
</pre><pre code_snippet_id="216524" snippet_file_name="blog_20140304_1_4789227" name="code" class="html">最近开始关注通知流程,写个小例子:<pre code_snippet_id="216524" snippet_file_name="blog_20140304_1_4789227" name="code" class="html">public class MainActivity extends Activity {public Button mButtonSend;
public Button mButtonCancel;
public Button mButtonOther;
public TextView mTextSend;
public TextView mTextCancel;
public TextView mTextOther;
public NotificationManager mNotiManager;
public Notification mNoti;
private PendingIntent mPDIntent;
public static final int NOTIFICATIO_TEST_ID = 1;
public static final int NOTIFICATIO_TEST_ID_OTHER = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
</pre><pre code_snippet_id="216524" snippet_file_name="blog_20140304_18_8119151" name="code" class="html">private void init() {
mButtonSend = (Button) findViewById(R.id.button1);
mButtonCancel = (Button) findViewById(R.id.button2);
mButtonOther = (Button) findViewById(R.id.button3);
mTextSend = (TextView) findViewById(R.id.textView1);
mTextCancel = (TextView) findViewById(R.id.textView2);
mTextOther = (TextView) findViewById(R.id.textView3);
mButtonSend.setOnClickListener(buttonListener);
mButtonCancel .setOnClickListener(buttonListener);
mButtonOther .setOnClickListener(buttonListener);
mNotiManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this, pd_intent.class);
mPDIntent = PendingIntent.getActivity(MainActivity.this, 0, mIntent, 0);
}
private OnClickListener buttonListener =new View.OnClickListener(){
    public void onClick(View v){
        switch (v.getId()) {
           case R.id.button1:
                sendNotification(); 
                break;
            case R.id.button2: 
                cancelNotification(); 
                break;
            case R.id.button3: 
                sendNotificationOther(); 
                break;
            default: 
                break;
      }
    }
private void sendNotificationOther() {
mNoti = new Notification();
mNoti.icon = R.drawable.ic_launcher;
mNoti.tickerText = "haha ?";
mNoti.when = System.currentTimeMillis();
//<span style="font-family: Arial, Helvetica, sans-serif;">mNoti.flags |= Notification.FLAG_AUTO_CANCEL;//点击通知界面跳转后自动清除通知,不跳转不取消</span>
<span style="font-family: Arial, Helvetica, sans-serif;">mNoti.setLatestEventInfo(MainActivity.this, "title", "点击跳转,通知不能自动清除", mPDIntent);</span>
mNotiManager.notify(NOTIFICATIO_TEST_ID_OTHER, mNoti);
}
</pre><pre code_snippet_id="216524" snippet_file_name="blog_20140304_58_7090701" name="code" class="html">取消通知:
</pre><pre code_snippet_id="216524" snippet_file_name="blog_20140304_59_7407129" name="code" class="html">private void cancelNotification() {
mNotiManager.cancelAll();//取消该NotificationManager管理的所有通知
//mNotiManager.cancel(NOTIFICATIO_TEST_ID);//取消该NotificationManager管理的NOTIFICATIO_TEST_ID通知
}
</pre><pre code_snippet_id="216524" snippet_file_name="blog_20140304_64_4529164" name="code" class="html">发送通知:
</pre><pre code_snippet_id="216524" snippet_file_name="blog_20140304_65_6274050" name="code" class="html">private void sendNotification() {
mNoti = new Notification();
mNoti.icon = R.drawable.ic_launcher;
mNoti.tickerText = "This is test for notification,are you right?";
mNoti.when = System.currentTimeMillis();
mNoti.flags |= Notification.FLAG_AUTO_CANCEL;//点击通知界面跳转后自动清除通知
mNoti.setLatestEventInfo(MainActivity.this,"TITLE", "点击跳转,通知自动清除", mPDIntent);
mNotiManager.notify(NOTIFICATIO_TEST_ID, mNoti);
}
};


public class MainActivity extends Activity {public Button mButtonSend;
public Button mButtonCancel;
public Button mButtonOther;
public TextView mTextSend;
public TextView mTextCancel;
public TextView mTextOther;
public NotificationManager mNotiManager;
public Notification mNoti;
private PendingIntent mPDIntent;
public static final int NOTIFICATIO_TEST_ID = 1;
public static final int NOTIFICATIO_TEST_ID_OTHER = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
</pre><pre code_snippet_id="216524" snippet_file_name="blog_20140304_93_4447096" name="code" class="html">private void init() {
mButtonSend = (Button) findViewById(R.id.button1);
mButtonCancel = (Button) findViewById(R.id.button2);
mButtonOther = (Button) findViewById(R.id.button3);
mTextSend = (TextView) findViewById(R.id.textView1);
mTextCancel = (TextView) findViewById(R.id.textView2);
mTextOther = (TextView) findViewById(R.id.textView3);
mButtonSend.setOnClickListener(buttonListener);
mButtonCancel .setOnClickListener(buttonListener);
mButtonOther .setOnClickListener(buttonListener);
mNotiManager = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Intent mIntent = new Intent(this, pd_intent.class);
mPDIntent = PendingIntent.getActivity(MainActivity.this, 0, mIntent, 0);
}
private OnClickListener buttonListener =new View.OnClickListener(){
    public void onClick(View v){
        switch (v.getId()) {
           case R.id.button1:
                sendNotification(); 
                break;
            case R.id.button2: 
                cancelNotification(); 
                break;
            case R.id.button3: 
                sendNotificationOther(); 
                break;
            default: 
                break;
      }
    }
private void sendNotificationOther() {
mNoti = new Notification();
mNoti.icon = R.drawable.ic_launcher;
mNoti.tickerText = "haha ?";
mNoti.when = System.currentTimeMillis();
//<span style="font-family: Arial, Helvetica, sans-serif;">mNoti.flags |= Notification.FLAG_AUTO_CANCEL;//点击通知界面跳转后自动清除通知,不跳转不取消</span>
<span style="font-family: Arial, Helvetica, sans-serif;">mNoti.setLatestEventInfo(MainActivity.this, "title", "点击跳转,通知不能自动清除", mPDIntent);</span>
mNotiManager.notify(NOTIFICATIO_TEST_ID_OTHER, mNoti);
}
</pre><pre code_snippet_id="216524" snippet_file_name="blog_20140304_133_9973083" name="code" class="html">private void cancelNotification() {
mNotiManager.cancelAll();//取消该NotificationManager管理的所有通知
//mNotiManager.cancel(NOTIFICATIO_TEST_ID);//取消该NotificationManager管理的NOTIFICATIO_TEST_ID通知
}
</pre><pre code_snippet_id="216524" snippet_file_name="blog_20140304_138_2078608" name="code" class="html">private void sendNotification() {
mNoti = new Notification();
mNoti.icon = R.drawable.ic_launcher;
mNoti.tickerText = "This is test for notification,are you right?";
mNoti.when = System.currentTimeMillis();
mNoti.flags |= Notification.FLAG_AUTO_CANCEL;//点击通知界面跳转后自动清除通知
mNoti.setLatestEventInfo(MainActivity.this,"TITLE", "点击跳转,通知自动清除", mPDIntent);
mNotiManager.notify(NOTIFICATIO_TEST_ID, mNoti);
}
};
附:
<p>下拉每条通知布局:notification_template_base.xml</p><p>xml<span style="font-family:宋体;">路径:</span><span style="font-family:Calibri;">framework/base/core/res/res/layout/</span></p>

                                             
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 小车前轮刹车卡钳卡死了怎么办 耐克气垫鞋扎了怎么办 骑自行车右大腿根痛怎么办 骑车骑得膝盖疼怎么办 骑了自行车腿疼怎么办 群面没有被问题怎么办 群面没有被提问怎么办 校招解约学校不盖章怎么办 科目三老是跑偏怎么办 面试官说你不好怎么办 面试紧张心态调整不过来怎么办 面试官问缺乏经验怎么办 办好入职手续后就想辞职怎么办 新店长入职应该怎么办 刚入职怀孕了辞职店长不同意怎么办 派遣证丢了怎么办补办 出国留学回来怎么办派遣证 报到证坏了一边怎么办 江苏报到证丢了怎么办 河南报到证丢了怎么办 报到证丢失10年怎么办 退休时无派遣证怎么办 档案里没有派遣证怎么办 档案中派遣证丢失怎么办 中专毕业后想读大学该怎么办 软考准考证丢了怎么办 面试薪资要低了怎么办 应聘等通知没有电话回复怎么办 学生信息表里的籍贯不对怎么办? 小学生信息表里的籍贯不对怎么办? 钉钉下班没打卡怎么办 如果在厂里饭卡丢了怎么办 工作上做错了事想辞职怎么办 结婚辞职怀孕感觉做错了怎么办 换新手机了钉钉怎么办 公众号申请链接无效怎么办 指纹打卡机时间调错已打卡怎么办 智慧云平台先锋讲座打不开怎么办 下雨穿套裙工装好冷怎么办 国家债务违约不要国际融资怎么办 欠钱的确还不起怎么办