自定义通知无法在Android Wear上运行

时间:2014-09-27 09:37:00

标签: android wear-os

我正在玩Wear SDK并尝试创建一个磨损应用程序。 我想显示自定义通知as shown on android docs,但它不起作用。

这是我的活动代码:

public class WearActivity extends Activity {
    private Button notifyBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wear);
        final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);

        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {

            @Override
            public void onLayoutInflated(WatchViewStub stub) {
                notifyBtn = (Button) stub.findViewById(R.id.notifyBtn);
                notifyBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        createNotification();

                    }
                });
            }
        });
    }

    private void createNotification(){
        Toast.makeText(this, "Press", Toast.LENGTH_LONG).show();
        //Create Intent
        Intent notificationIntent =
                new Intent(this, WearNotificationActivity.class)
                        .putExtra("EXTRA_STRING", "Hi, I'm an EXTRA!");
        PendingIntent pendingNotificationIntent =
                PendingIntent.getActivity(this,0,notificationIntent,PendingIntent.FLAG_UPDATE_CURRENT);

        Notification notification =
                new Notification.Builder(this)
                        .extend(new Notification.WearableExtender()
                                .setDisplayIntent(pendingNotificationIntent)
                                .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
                        .build();

        NotificationManager notificationManager =
                (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);

        notificationManager.notify(0, notification);
    }
}

虽然按下了按钮,但不会显示自定义通知。 我编辑了Manifest Too:

    <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.tizianobasile.wearactivity" >

    <uses-feature android:name="android.hardware.type.watch" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.DeviceDefault.Light" >
        <activity
            android:name=".WearActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".WearNotificationActivity"
            android:label="@string/title_activity_wear_notification"
            android:exported="true"
            android:allowEmbedded="true"
            android:taskAffinity=""
            android:theme="@android:style/Theme.DeviceDefault.Light">
        </activity>
    </application>

</manifest>

我真的无法找到原因,我的代码几乎与文档代码相同。

1 个答案:

答案 0 :(得分:3)

一般情况下,Android Wear中的通知必须使用smallIcon。从我的测试来看,尽管在自定义卡片布局(setDisplayIntent)的通知中,图标甚至没有显示 - 您仍然需要指定它才能在Android Wear上显示。

例如:

Notification notification =
    new Notification.Builder(this)
        .setSmallIcon(R.drawable.ic_launcher)
        .extend(new Notification.WearableExtender()
            .setDisplayIntent(pendingNotificationIntent)
            .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM))
        .build();