最近很多小伙伴都在问调用活动被销毁时,新活动上的AndroidgetIntent为NULL这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展Android11上的Android通知s
最近很多小伙伴都在问调用活动被销毁时,新活动上的Android getIntent为NULL这两个问题,那么本篇文章就来给大家详细解答一下,同时本文还将给你拓展Android 11 上的 Android 通知 setFullScreenIntent 操作按钮不需要的边框、Android 4.4 (KitKat) 上的 Android Gallery 为 Intent.ACTION_GET_CONTENT 返回不同的 URI、Android onNewIntent Uri始终为null、android – DialogFragment如何影响调用活动的生命周期等相关知识,下面开始了哦!
本文目录一览:- 调用活动被销毁时,新活动上的Android getIntent为NULL
- Android 11 上的 Android 通知 setFullScreenIntent 操作按钮不需要的边框
- Android 4.4 (KitKat) 上的 Android Gallery 为 Intent.ACTION_GET_CONTENT 返回不同的 URI
- Android onNewIntent Uri始终为null
- android – DialogFragment如何影响调用活动的生命周期
调用活动被销毁时,新活动上的Android getIntent为NULL
你们有以下问题的解决方法吗?
在注册活动的onDestroy中(当用户按下后退按钮时),我调用一个新活动,以便用户可以输入当天的某些最终生产数据,然后通过电子邮件发送该报告。问题在于,在刚刚开始的活动中,对getIntent的调用返回null,而我必须从那里获取数据。
public void onDestroy(){ //unregister listeners, cancel timers etc. logOff(); super.onDestroy();}protected void logOff(){ // collect data etc. // open new activity that asks for final production numbers Intent intent = new Intent(getBaseContext(), AksProductionNumbers.class); intent.putExtra("TimeSheetList", timeSheetList); startActivity(intent);}
==================================================
============================构造函数称为活动:
public AksProductionNumbers(){ Intent intent = getIntent(); // <-- returns null Bundle extras = intent.getExtras();}
函数getIntent返回null。我认为这是因为调用活动已经死了,但我认为这很奇怪,因为我明确要求框架在关闭当前活动之前启动新活动。有谁知道解决此问题的方法?我选择了此解决方案,因此不必覆盖“后退”按钮。另一个解决方案是阻塞调用线程,直到它从启动的应用程序获得取消阻塞信号为止,但我认为这也很丑陋。第三类数据管理器也可以解决数据源问题。感谢您的想法和建议!
答案1
小编典典实际上,我想我知道您的问题是什么。我相信您正在拨打此电话:
public AksProductionNumbers()
在活动 构造函数中 (如您在问题中所指出的),应在活动的onCreate
方法中进行设置。getIntent
将在构造函数中返回null。
Android 11 上的 Android 通知 setFullScreenIntent 操作按钮不需要的边框
如何解决Android 11 上的 Android 通知 setFullScreenIntent 操作按钮不需要的边框?
在具有 setFullScreenIntent
的 Android 11 上显示通知时,通知中的操作按钮会出现烦人的边框:
在 Android 10 上运行相同的代码或在 Android 11 上删除 setFullScreenIntent
时,通知设计将按预期进行:
我的代码供参考:
private void showNotification() {
notificationmanager androidnotificationmanager = (notificationmanager) getSystemService(Context.NOTIFICATION_SERVICE);
createHeadsUpNotificationChannel(androidnotificationmanager);
RemoteViews contentView = new RemoteViews(getPackageName(),R.layout.layout_sample_notif);
contentView.setTextViewText(R.id.id1,"Header");
contentView.setTextViewText(R.id.id2,"Body");
NotificationCompat.Builder builder = new NotificationCompat.Builder(this,NOTIFICATION_CHANNEL);
builder.setVisibility(Notification.VISIBILITY_PUBLIC)
.setContentTitle("Sample Notification")
.setSmallIcon(R.drawable.sample_icon)
.setStyle(new NotificationCompat.DecoratedCustomViewStyle())
.setCustomContentView(contentView)
.setGroup(GROUP_KEY)
.setCategory(NotificationCompat.CATEGORY_ALARM).setPriority(NotificationCompat.PRIORITY_HIGH)
.setDefaults(NotificationCompat.DEFAULT_ALL);
Intent fullScreenIntent = new Intent(this,FullscreenActivity.class);
fullScreenIntent.setAction(String.valueOf(System.currentTimeMillis()));
fullScreenIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent fullScreenPendingIntent = PendingIntent.getActivity(this,fullScreenIntent,PendingIntent.FLAG_UPDATE_CURRENT);
builder.setFullScreenIntent(fullScreenPendingIntent,true);
Intent dismissIntent = new Intent(this,MyService.class);
dismissIntent.setAction("action_dismiss");
builder.setongoing(true)
.addAction(R.drawable.sample_icon,"dismiss",PendingIntent.getService(this,dismissIntent,PendingIntent.FLAG_ONE_SHOT));
androidnotificationmanager.notify(0,builder.build());
}
private void createHeadsUpNotificationChannel(notificationmanager androidnotificationmanager) {
NotificationChannel channel = new NotificationChannel(NOTIFICATION_CHANNEL,"Sample Channel",notificationmanager.IMPORTANCE_HIGH);
channel.setDescription("Sample Desc");
channel.enableLights(false);
channel.enableVibration(false);
androidnotificationmanager.createNotificationChannel(channel);
}
}
我需要帮助为我的通知设计一致的材料。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
Android 4.4 (KitKat) 上的 Android Gallery 为 Intent.ACTION_GET_CONTENT 返回不同的 URI
在 KitKat 之前(或在新画廊之前)Intent.ACTION_GET_CONTENT
返回一个这样的 URI
内容://媒体/外部/图像/媒体/3951。
使用ContentResolver
和查询 MediaStore.Images.Media.DATA
返回的文件 URL。
然而,在 KitKat 中,Gallery 会返回一个 URI(通过“Last”),如下所示:
内容://com.android.providers.media.documents/document/image:3951
我该如何处理?
答案1
小编典典尝试这个:
if (Build.VERSION.SDK_INT <19){ Intent intent = new Intent(); intent.setType("image/jpeg"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, getResources().getString(R.string.select_picture)),GALLERY_INTENT_CALLED);} else { Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); intent.addCategory(Intent.CATEGORY_OPENABLE); intent.setType("image/jpeg"); startActivityForResult(intent, GALLERY_KITKAT_INTENT_CALLED);}@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode != Activity.RESULT_OK) return; if (null == data) return; Uri originalUri = null; if (requestCode == GALLERY_INTENT_CALLED) { originalUri = data.getData(); } else if (requestCode == GALLERY_KITKAT_INTENT_CALLED) { originalUri = data.getData(); final int takeFlags = data.getFlags() & (Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION); // Check for the freshest data. getContentResolver().takePersistableUriPermission(originalUri, takeFlags); } loadSomeStreamAsynkTask(originalUri);}
大概需要
@SuppressLint(“NewApi”)
为了
获取PersistableUriPermission
Android onNewIntent Uri始终为null
这个活动就像这个context.startActivity一样启动浏览器(new Intent(Intent.ACTION_VIEW,Uri.parse(requestToken.getAuthenticationURL())));
并返回此处
@Override public void onNewIntent(Intent intent){ super.onNewIntent(intent); Uri uri = getIntent().getData(); if (uri != null && uri.toString().startsWith(TwitterConstants.CALLBACK_URL)) {...} }
但是uri总是空的.
明显的东西:
<activity android:name="myapp.mypackage.TweetFormActivity" android:configChanges="orientation|keyboardHidden" android:label="@string/app_name" android:launchMode="singleInstance" android:screenorientation="portrait" android:theme="@android:style/Theme.Black.NoTitleBar"> <intent-filter> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" /> <category android:name="android.intent.category.broWSABLE" /> <data android:scheme="oauth" android:host="myapp"/> </intent-filter> </activity> static final String CALLBACK_URL = "oauth://myapp";
我在这里想念的是什么?谢谢
解决方法
更新:所以,这里有两种方法可以实现onNewIntent().第一个用新的意图替换旧意图,所以当你稍后调用getIntent()时,你将收到新意图.
@Override protected void onNewIntent(final Intent intent) { super.onNewIntent(intent); // Here we''re replacing the old intent with the new one. setIntent(intent); // Now we can call getIntent() and receive the new intent. final Uri uri = getIntent().getData(); // Do something with the URI... }
第二种方法是使用来自新意图的数据,保留旧的意图.
@Override protected void onNewIntent(final Intent intent) { super.onNewIntent(intent); // We do not call setIntent() with the new intent,// so we have to retrieve URI from the intent argument. final Uri uri = intent.getData(); // Do something with the URI... }
当然,您可以使用两种变体的组合,但不要期望从getIntent()接收新意图,直到您使用setIntent()显式设置它.
android – DialogFragment如何影响调用活动的生命周期
假设启动我的片段的方法是
private void launchFragment(){ ConfirmationDialog confirm = new ConfirmationDialog(); confirm.show(getSupportFragmentManager(),"confirm"); doMoreStuff(); }
所以我的问题是双重的:
>什么时候调用doMoreStuff()?在关闭片段之前或之后返回父活动?
>关闭片段以返回父活动后,父活动是否通过onResume:这样如果我检查片段更改了某个字段,我可以根据该字段的状态在onResume中工作:
如下例所示:
@Override public void onResume() { super.onResume(); if(dialogFragmentChangedThis){ workSomeMore(); } }
片段是用.启动的
setStyle(STYLE_NO_FRAME,android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
这样它就处于全屏模式.
解决方法
When is doMoreStuff() called? Before or after I close the fragment to return to the parent activity?
DialogFragment的show()方法只提交一个FragmentTransaction,它将异步执行,因此后面的任何方法都会立即执行.
After I close the fragment to return to the parent activity,does the parent activity go through onResume
不,它没有,因为您的活动始终处于前台,因为没有涉及其他活动.
通常,您将使用带有DialogFragments的Callbacks侦听器,就像使用简单片段一样,通过在Activity中实现Callbacks,并在想要传递DialogFragment中发生的操作结果时通过该接口调用Activity的方法.
关于调用活动被销毁时,新活动上的Android getIntent为NULL的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于Android 11 上的 Android 通知 setFullScreenIntent 操作按钮不需要的边框、Android 4.4 (KitKat) 上的 Android Gallery 为 Intent.ACTION_GET_CONTENT 返回不同的 URI、Android onNewIntent Uri始终为null、android – DialogFragment如何影响调用活动的生命周期的相关知识,请在本站寻找。
本文标签: