-1

我正在为 Android Wear 开发一个自定义通知,当通知向上滑动时应该显示一个自定义活动。通知会显示,甚至可穿戴功能(如背景和自定义大小)都是可见的,但活动从未加载或显示。

此外,MainActivity并且PlaybackNotificationActivity应该在创建它们时将消息写入日志。MainActivity写消息,但后者不写。

可能是什么问题呢?

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new NotificationBuilder(this).showNotification();
        Log.e("MainActivity", "Reached");
        finish();
    }
}

NotificationBuilder.java

public class NotificationBuilder {
    private final Context mContext;

    public NotificationBuilder(Context context) {
        mContext = context;
    }

    public Notification buildNotification() {
        Notification.Builder builder = new Notification.Builder(mContext)
                .setContentTitle("Title")
                .setContentText("Content text")
                .setSmallIcon(R.mipmap.ic_launcher);

        Intent playbackIntent = new Intent(mContext, PlaybackNotificationActivity.class);

        PendingIntent playbackPendingIntent = PendingIntent.getActivity(
                mContext, 0, playbackIntent, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification.WearableExtender extender = new Notification.WearableExtender()
                .setBackground(BitmapFactory.decodeResource(mContext.getResources(), R.mipmap.ic_launcher))
                .setCustomSizePreset(Notification.WearableExtender.SIZE_MEDIUM)
                .setDisplayIntent(playbackPendingIntent);

        builder.extend(extender);

        return builder.build();
    }

    public void showNotification() {
        Notification notification = buildNotification();

        NotificationManager manager = (NotificationManager)
                mContext.getSystemService(Context.NOTIFICATION_SERVICE);

        manager.notify(1, notification);
    }
}

PlaybackNotificationActivity.java

public class PlaybackNotificationActivity extends Activity {

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

        Log.e("NotificationActivity", "Reached");
        Toast.makeText(this,  "Toast", Toast.LENGTH_SHORT).show();
    }
}
4

1 回答 1

0

事实证明,Android Wear 模拟器是罪魁祸首,它在物理智能手表上完美运行,但在模拟器上却不行。

于 2015-07-08T09:58:17.137 回答