Android系统 小米/三星/索尼快键图标BadgeNumber数字提醒

Android系统 小米,三星,索尼手机发送桌面快键提醒数字图标,在Android系统中,众所周知不支持BadgeNumber,虽然第三方控件BadgeView可以实现应用内的数字提醒,但对于系统的图标,特别是app的logo图标很难实现数字标志,即使是绘图的方式不断修改,但这种方式天生弊端,实用性很差。

我们现在来实现桌面logo或者说icon右上角的图标,先来看2张图,第一张来自互联网,第二张来自个人实践!(由于实验条件youxia)

   

好了,上代码

public class MainActivity extends Activity {
      //必须使用,Activity启动页
      private final static String lancherActivityClassName = Welcome.class.getName();
      
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.common_listview_layout);
	}

	@Override
	protected void onResume() {
		super.onResume();
		sendBadgeNumber();
	}

	private void sendBadgeNumber() {
		String number = "35";
		if (TextUtils.isEmpty(number)) {
			number = "";
		} else {
			int numInt = Integer.valueOf(number);
			number = String.valueOf(Math.max(0, Math.min(numInt, 99)));
		}

		if (Build.MANUFACTURER.equalsIgnoreCase("Xiaomi")) {
			sendToXiaoMi(number);
		} else if (Build.MANUFACTURER.equalsIgnoreCase("samsung")) {
			sendToSony(number);
		} else if (Build.MANUFACTURER.toLowerCase().contains("sony")) {
			sendToSamsumg(number);
		} else {
			Toast.makeText(this, "Not Support", Toast.LENGTH_LONG).show();
		}
	}

	private void sendToXiaoMi(String number) {
		try {
			Class miuiNotificationClass = Class.forName("android.app.MiuiNotification");
			Object miuiNotification = miuiNotificationClass.newInstance();
			Field field = miuiNotification.getClass().getDeclaredField("messageCount");
			field.setAccessible(true);
			field.set(miuiNotification, number);// 设置信息数-->这种发送必须是miui 6才行
		} catch (Exception e) {
		    e.printStackTrace();
		    //miui 6之前的版本
    		    Intent localIntent = new Intent("android.intent.action.APPLICATION_MESSAGE_UPDATE");
    		    localIntent.putExtra("android.intent.extra.update_application_component_name",getPackageName() + "/"+ lancherActivityClassName );
    		    localIntent.putExtra("android.intent.extra.update_application_message_text",number);
    		    sendBroadcast(localIntent);
		}

	}

	private void sendToSony(String number) {
		boolean isShow = true;
		if ("0".equals(number)) {
			isShow = false;
		}
		Intent localIntent = new Intent();
		localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.SHOW_MESSAGE",isShow);//是否显示
		localIntent.setAction("com.sonyericsson.home.action.UPDATE_BADGE");
		localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.ACTIVITY_NAME",lancherActivityClassName );//启动页
		localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.MESSAGE", number);//数字
		localIntent.putExtra("com.sonyericsson.home.intent.extra.badge.PACKAGE_NAME",getPackageName());//包名
		sendBroadcast(localIntent);

		Toast.makeText(this, "Sony," + "isSendOk", Toast.LENGTH_LONG).show();
	}

	private void sendToSamsumg(String number) 
	{
		Intent localIntent = new Intent("android.intent.action.BADGE_COUNT_UPDATE");
		localIntent.putExtra("badge_count", number);//数字
		localIntent.putExtra("badge_count_package_name", getPackageName());//包名
		localIntent.putExtra("badge_count_class_name",lancherActivityClassName ); //启动页
		sendBroadcast(localIntent);
		Toast.makeText(this, "Samsumg," + "isSendOk", Toast.LENGTH_LONG).show();
	}
}

注意lancherActivityClassName 必须被配置为 启动页   android.intent.category.LAUNCHER

 <activity
            android:name="com.sample.activites.Welcome"
            android:configChanges="locale|keyboard|screenSize"
            android:label="@string/app_name"
            android:screenOrientation="portrait" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.CREATE_SHORTCUT" />
            </intent-filter>
        </activity>



try doing it

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。