我的情况很奇怪.
有一个应用程序,我决定从第一个应用程序的代码创建另一个.
我复制了.xml文件,复制了.java文件以便一切正常.
但是有一个巨大的问题:我的onNewIntent(Intent intent)方法是在第一个项目中调用的,但它没有在第二个项目中调用(代码是相同的!)
方法,可以触发,但现在无法触发
public void onClick(View arg0) {
Intent browserInt = new Intent (Intent.ACTION_VIEW,
Uri.parse("https://oauth.yandex.ru/authorize?response_type=token&client_id=zzzzz"));
browserInt.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(browserInt);
}
Run Code Online (Sandbox Code Playgroud)
这是onNewIntent()方法:
@Override
protected void onNewIntent(Intent intent){
System.out.println(" I WORKED!");
Uri uri = intent.getData();
if (uri!=null) {
String m = uri.toString().split("#")[1];
String[] args = m.split("&");
String arg = args[0];
String token = arg.split("=")[1];
System.out.println(token);
}
}
Run Code Online (Sandbox Code Playgroud)
遗憾的是,我在日志中看不到"我工作".
我在SO和Internet上都阅读了很多类似的问题,尝试设置Intent标志SINGLE_TOP,SINGLE_TASK等等.
这是Android Manifest的WORKING项目:
<application
android:name="yyy"
android:icon="@drawable/yaru_icon"
android:allowBackup="false"
android:label="xxx"
android:theme="@style/LightTheme">
<activity
android:name=".Main"
android:label="xxx"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application> …Run Code Online (Sandbox Code Playgroud) 我看到了几种方法,我尝试了一切,但无法使它工作.我不知道为什么它如此复杂,在文档中它看起来如此简单!我想通过通知触发OnNewIntent(用户在通知栏中单击它).
目前我已将Activity设置为singleTop
<activity
android:name="-.-.-.MainMenuActivity"
android:launchMode="singleTop"
android:screenOrientation="portrait" >
</activity>
Run Code Online (Sandbox Code Playgroud)
这是我创建通知的代码:
public void showNotification(String text, String componentId){
NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.logo)
.setContentTitle("Title")
.setAutoCancel(true)
.setContentText(text);
// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, MainMenuActivity.class);
if(!componentId.equalsIgnoreCase("")){
resultIntent.putExtra("componentId", componentId);
}
resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent resultPendingIntent = PendingIntent.getActivity(this, 0, resultIntent, 0);
mBuilder.setFullScreenIntent(resultPendingIntent, false);
mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// mId allows you to update the notification later on.
mNotificationManager.notify(0, mBuilder.build());
}
Run Code Online (Sandbox Code Playgroud)
这是我的MainMenuActivity中的OnNewIntent方法:
@Override
protected void …Run Code Online (Sandbox Code Playgroud)