2014-09-27 62 views
0

我正在使用Wear SDK並嘗試創建磨損應用程序。 我想顯示一個自定義通知as shown on android docs但它不起作用。自定義通知不會在Android Wear上運行

這是我的活動代碼:

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); 
    } 
} 

雖然按下按鈕被觸發時,沒有出現自定義通知。 我編輯的清單太:

<?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> 

我實在找不出原因,我的代碼幾乎是相同的文件代碼。

回答

3

通常,smallIcon是Android Wear中的通知必需的。從我的測試中,儘管在通知中使用自定義卡片佈局(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(); 
+0

有什麼奇怪的行爲!它在加入smallIcon之後起作用......非常感謝Maciej – basteez 2014-09-29 16:29:01