statusbar—NOTIFICATION

来源:互联网 发布:模拟退火算法模型 编辑:程序博客网 时间:2024/04/20 18:59
</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
原创粉丝点击