我正在为 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();
}
}