要实现在Android中点击通知打开文件,按返回键不会显示应用程序的效果,可以通过以下步骤实现:
// 创建一个通知
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
.setSmallIcon(R.drawable.ic_notification)
.setContentTitle("文件通知")
.setContentText("点击打开文件");
// 创建一个Intent,用于打开文件
Intent intent = new Intent(Intent.ACTION_VIEW, fileUri);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
// 为通知设置点击事件
builder.setContentIntent(pendingIntent);
// 发送通知
NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(notificationId, builder.build());
onCreate()方法中获取文件URI,并根据文件类型打开相应的应用程序。public class FileActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_file);
// 获取文件URI
Uri fileUri = getIntent().getData();
// 根据文件类型打开相应的应用程序
Intent openIntent = new Intent(Intent.ACTION_VIEW);
openIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
openIntent.setDataAndType(fileUri, getContentResolver().getType(fileUri));
startActivity(openIntent);
// 关闭当前Activity
finish();
}
}
通过以上步骤,点击通知将会打开文件,并且按返回键时不会显示应用程序的界面。
上一篇:Android - 点击提交按钮后,从不同的活动接收到错误消息
下一篇:Android - 调用androidx凭据管理器PASSKEYS的“getCredentialAsync()”时出现“NoCredentialException”