今天試著把launcher icon 加了數字提示,找到一篇可以用的,
在我的LG手機上測試正常
程式碼
import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.TextView; import java.util.List; public class MainActivity extends ActionBarActivity { int count=0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button sent=(Button)findViewById(R.id.set); Button reset=(Button)findViewById(R.id.reset); final TextView text=(TextView)findViewById(R.id.textView); sent.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { text.setText(String.valueOf(++count)); setBadge(MainActivity.this,count); } }); reset.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { text.setText("0"); count=0; setBadge(MainActivity.this,count); } }); } public static void setBadge(Context context, int count) { String launcherClassName = getLauncherClassName(context); if (launcherClassName == null) { return; } Intent intent = new Intent("android.intent.action.BADGE_COUNT_UPDATE"); intent.putExtra("badge_count", count); intent.putExtra("badge_count_package_name", context.getPackageName()); intent.putExtra("badge_count_class_name", launcherClassName); context.sendBroadcast(intent); } public static String getLauncherClassName(Context context) { PackageManager pm = context.getPackageManager(); Intent intent = new Intent(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); List<ResolveInfo> resolveInfos = pm.queryIntentActivities(intent, 0); for (ResolveInfo resolveInfo : resolveInfos) { String pkgName = resolveInfo.activityInfo.applicationInfo.packageName; if (pkgName.equalsIgnoreCase(context.getPackageName())) { String className = resolveInfo.activityInfo.name; return className; } } return null; } }
Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" > <TextView android:text="@string/hello_world" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/textView" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Set Bage" android:id="@+id/set" android:layout_below="@+id/textView" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Reset Bage" android:id="@+id/reset" android:layout_below="@+id/set" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" /> </RelativeLayout>
執行結果
參考連結中的第二篇有提到一點要注意。
注意launcherClassName 必須被設定成
android.intent.category.LAUNCHER
參考資料:
http://stackoverflow.com/questions/17565307/how-to-display-count-of-notifications-in-app-launcher-icon
http://my.oschina.net/ososchina/blog/352286
沒有留言:
張貼留言