java手机状态栏圆形图标,Android 软件的状态栏图标通知处理

编写:徐建祥(netpirate@gmail.com)

日期:2010/11/22

网址:http://www.anymobile.org

QQ的状态栏通知机制:当所有QQ的UI Activity切换到后台后,添加状态通知;切换回来后,删除该状态通知。

飞信的状态栏通知方式:运行软件后,图标一直显示在状态栏的通知栏中;显示退出软件则删除该状态通知。

似乎QQ的更有点技术含量,多个程序切换到后台的处理而已;以飞信的模式,做个类似的测试,案例如下:

程序路径:org.anymobile.im

程序入口:org.anymobile.im.LoginActivity(Action:android.intent.action.MAIN;Category:android.intent.category.LAUNCHER)

主界面程序:org.anymobile.im.MainActivity

测试程序流程:未登录的情况下,或者第一次运行会打开LoginActivity程序;登陆后,一直停留在主界面MainActivity。

所以,通过Notification,需可以回到im项目的上一个界面程序,LoginActivity / MainActivity,这里就要参考Launcher应用的相关实现,Intent的flag设置。

测试代码,新建一个android项目,TestNotification,入口程序:TestActivity,代码如下:

packageorg.anymobile.test;

importandroid.app.Activity;

importandroid.content.ComponentName;

importandroid.content.Intent;

importandroid.graphics.LightingColorFilter;

importandroid.os.Bundle;

importandroid.view.Menu;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

publicclassTestActivityextendsActivity

{

privatestaticfinalintADD_ID =0;

privatestaticfinalintDEL_ID =1;

/** Called when the activity is first created. */

@Override

publicvoidonCreate(Bundle savedInstanceState)

{

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

Button button = (Button) this.findViewById(R.id.btn_menu);

button.setOnClickListener(newOnClickListener()

{

publicvoidonClick(View v)

{

//              TestActivity.this.openOptionsMenu();

String packName = "org.anymobile.im";

String className = packName + ".LoginActivity";

Intent intent = newIntent();

ComponentName componentName = newComponentName(packName, className);

intent.setComponent(componentName);

intent.setAction("android.intent.action.MAIN");

intent.addCategory("android.intent.category.LAUNCHER");

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

TestActivity.this.startActivity(intent);

}

});

//        button.getBackground().setColorFilter(0xFFFF0000, PorterDuff.Mode.MULTIPLY);

button.getBackground().setColorFilter(newLightingColorFilter(0xFFFFFFFF,0xFFAA0000));

}

@Override

publicbooleanonCreateOptionsMenu(Menu menu)

{

menu.add(0, ADD_ID,0,"ADD");

menu.add(0, DEL_ID,0,"DEL");

returnsuper.onCreateOptionsMenu(menu);

}

}

OK,开始测试状态栏的通知功能:

1、LoginActivity.onCreate() 调用showNotification()方法,创建一个通知图标;

/**

* The notification is the icon and associated expanded entry in the

* status bar.

*/

protectedvoidshowNotification()

{

CharSequence from = "IM";

CharSequence message = "IM start up";

Intent intent = newIntent();

ComponentName componentName = newComponentName("com.longcheer.imm",

"com.longcheer.imm.activitys.LoginActivity");

intent.setComponent(componentName);

intent.setAction("android.intent.action.MAIN");

intent.addCategory("android.intent.category.LAUNCHER");

intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

intent.addFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);

// The PendingIntent to launch our activity if the user selects this notification

PendingIntent contentIntent = PendingIntent.getActivity(this,0, intent,0);

// construct the Notification object.

Notification notif = newNotification(R.drawable.icon,"IMM Still run background!",

System.currentTimeMillis());

notif.setLatestEventInfo(this, from, message, contentIntent);

// look up the notification manager service

NotificationManager nm = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);

nm.notify(R.string.app_name, notif);

}

2、在LoginActivity / MainAcitivity的退出操作中cancel该通知。

privatevoiddoExit()

{

this.finish();

NotificationManager nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);

nm.cancel(R.string.app_name);

}

测试OK!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值