Android Wear:通知上的自定义页面

时间:2015-09-24 14:43:59

标签: android notifications wear-os android-wear-notification

我是Android Wear(开发)的新手。我开始阅读并实施documentation

但是我不确定我想要实现的是“überhaupt”是否可行。 我可以在我收到的推送通知上附加自定义“操作”,但它似乎只能打开电话活动。为什么我不能开一个磨损活动?

推送通知包含最初显示的文本和有关足球比赛的数据(第二页?)。我希望在没有电话干预的情况下显示球队名称和得分。

有可能吗?

另外什么是默认行为?我是否将此附加到某个操作或通知上的额外页面?

NotificationCompat.Builder notificationBuilder =
    new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.ic_stat_notification_icon3)
            .setContentTitle(this.getString(R.string.notifications_title))
            .setContentText(message)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(message))
            .setContentIntent(matchDetailPendingIntent)
            .setAutoCancel(true)
            .extend(new NotificationCompat.WearableExtender()
                            .addPage(CustomDesignedPage) //Is this possible?
                            .addAction(action)
                            .setBackground(BitmapFactory.decodeResource(getResources(), R.drawable.soccer_background_big))
            );

修改

看看Messenger穿戴应用程序似乎有可能吗? Messenger Android Wear App

第二个屏幕显示了一个消息列表。

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题。我的解决方案是实现一个Activity并将自定义布局添加到此活动。请按照此步骤操作。

步骤1:在磨损模块中创建自定义布局。示例:customlayout.xml

步骤2:在磨损模块中创建一个活动:

    public class WearNotificationActivity extends Activity{

    private ImageView mSomeButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.customlayout);

        mSomeButton= (ImageView) this.findViewById(R.id.somebutton);



        mSomeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //do something here
            }
        });

    }

}

步骤3.将您想要的数据从手机发送到您的衣服:

    public class MainActivity extends Activity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener{

     private GoogleApiClient mGoogleApiClient;
     private Button mSomeButton;

    @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.mainlayout);
            mSomeButton=(Button) findViewById(R.id.somebutton);

            mGoogleApiClient = new GoogleApiClient.Builder(this)
                            .addApi(AppIndex.APP_INDEX_API)
                            .addApi(Wearable.API)
                            .addConnectionCallbacks(this)
                            .addOnConnectionFailedListener(this)
                            .build();
            mSomeButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
               sendToWear("title","description");
            }
        });

    }
    @Override
        public void onStart() {
            // TODO Auto-generated method stub
            super.onStart();
            if(!mGoogleApiClient.isConnected()) {
                mGoogleApiClient.connect();
            }

        }
    @Override
    public void onDestroy() {
        super.onDestroy();
        if(mGoogleApiClient!=null) {
            mGoogleApiClient.disconnect();
        }    

    }
    public void sendToWear(String title, String description){        
        PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/wear");
        putDataMapReq.getDataMap().putString("title", title);
        putDataMapReq.getDataMap().putString("description", description);                
        Wearable.DataApi.putDataItem(mGoogleApiClient, putDataRequest);
    }

}

步骤4.接收穿着中的数据并发出通知。为此,您必须在wear模块中创建一个类,该类扩展为WearableListenerService并将此类添加到您的磨损清单中。

 public class NotificationUpdateService extends WearableListenerService
        implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,
        ResultCallback<DataApi.DeleteDataItemsResult> {

    private GoogleApiClient mGoogleApiClient;

    @Override
    public void onCreate() {
        super.onCreate();
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(Wearable.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
    }


    @Override
    public void onDataChanged(DataEventBuffer dataEvents) {
        for (DataEvent dataEvent : dataEvents) {
            if (dataEvent.getType() == DataEvent.TYPE_CHANGED) {
                DataItem item = dataEvent.getDataItem();
                if (item.getUri().getPath().compareTo("/wear") == 0) {
                    DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
                    String title = dataMap.getString("title");
                    String description=dataMap.getString("description");
                    buildWearableOnlyNotification(title, description)

                }
            } else if (dataEvent.getType() == DataEvent.TYPE_DELETED) {

            }
        }
    }

    /**
     * Builds a simple notification on the wearable.
     */
    private void buildWearableOnlyNotification(String title, String content) {
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setOngoing(true)
                    .setPriority(NotificationCompat.PRIORITY_MAX)
                    .setVibrate(new long[]{10, 10, 10, 10, 10})
                    .setContentTitle(title)
                    .setContentText(content);
            Intent notificationIntent = new Intent(this, WearNotificationActivity.class);
            PendingIntent pendingNotificationIntent =
                    PendingIntent.getActivity(this, 0, notificationIntent, PendingIntent.FLAG_UPDATE_CURRENT);

            NotificationCompat.Builder secondpage =
                    new NotificationCompat.Builder(this)
                            .setSmallIcon(R.drawable.ic_launcher)
                            .extend(new NotificationCompat.WearableExtender()
                                            .setDisplayIntent(pendingNotificationIntent)
                                            .setCustomSizePreset(NotificationCompat.WearableExtender.SIZE_FULL_SCREEN)
                            );            
            mNotificationBuilder = new NotificationCompat.WearableExtender()
                    .addPage(secondpage.build()).extend(builder);
            Notification notification=mNotificationBuilder.build();
            ((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
                    .notify(Constants.WATCH_ONLY_ID, notification);
    }

    @Override
    public void onConnected(Bundle bundle) {

    }

    @Override
    public void onConnectionSuspended(int i) {
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
    }


}

在你的清单中:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.DeviceDefault" >
    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <activity
        android:name=".MainActivity"
        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:exported="true"
        android:allowEmbedded="true"
        android:taskAffinity=""
        android:theme="@android:style/Theme.DeviceDefault.Light"
        >
    </activity>
    <service android:name=".NotificationUpdateService">
        <intent-filter>
            <action
                android:name="com.google.android.gms.wearable.BIND_LISTENER" />
        </intent-filter>
    </service>
</application>

最后,您需要在手机中添加所有依赖项并佩戴gradle。

电话:

compile 'com.google.android.gms:play-services-wearable:7.5.0'
compile 'com.android.support:support-v4:23.1.0'
wearApp project(':wearmodule')

磨损:

compile 'com.google.android.support:wearable:1.3.0'
provided 'com.google.android.wearable:wearable:+'
compile 'com.google.android.gms:play-services-wearable:8.1.0'

我希望这对你有用。