gpt4 book ai didi

java - onNewIntent 没有被调用

转载 作者:行者123 更新时间:2023-12-02 04:09:06 27 4
gpt4 key购买 nike

我正在尝试使用 Intent 将推送数据从接收器传递到 Activity ,但它似乎不起作用。知道为什么吗?这是我的实现:

AndroidManifest.xml

<?xml version="1.0" encoding="UTF-8"?><!--

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CALL_PHONE" />

<permission android:protectionLevel="signature"
android:name="com.appName.permission.C2D_MESSAGE" />
<uses-permission android:name="com.appName.permission.C2D_MESSAGE" />


<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="RB"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:name="com.appName.appExtension">
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="API_KEY"
/>

<activity
android:name=".MainActivity"
android:label="RB"
android:theme="@style/AppTheme.NoActionBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>

<activity android:name="com.appNamw.RouteView"
android:label="@string/title_activity_route_view"
android:screenOrientation="landscape">

</activity>
<service android:name="com.parse.PushService" />
<receiver android:name="com.appName.ParseReceiver"
android:exported="false">
<intent-filter>
<action android:name="com.parse.push.intent.RECEIVE" />
<action android:name="com.parse.push.intent.DELETE" />
<action android:name="com.parse.push.intent.OPEN" />
</intent-filter>
</receiver>
<receiver android:name="com.parse.GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND">
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.appName" />
</intent-filter>
</receiver>
</application>

解析接收器

package com.appName;

import android.content.Context;
import android.content.Intent;
import android.nfc.Tag;
import android.util.Log;

import com.parse.ParseAnalytics;
import com.parse.ParsePushBroadcastReceiver;

import org.json.JSONException;
import org.json.JSONObject;


public class ParseReceiver extends ParsePushBroadcastReceiver {
private final String TAG = "Push_Notify";

@Override
public void onPushOpen(Context context, Intent intent) {
Log.i(TAG, "onPushOpen triggered!");

ParseAnalytics.trackAppOpenedInBackground(intent);
Intent mainIntent = new Intent(context, MainActivity.class);
mainIntent.putExtras(intent.getExtras());
mainIntent.putExtra("method", "updateStatus");

mainIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);


JSONObject pushData;

String alert;
try {
pushData = new JSONObject(intent.getStringExtra(ParseSupplyReceiver.KEY_PUSH_DATA));
alert = pushData.getString("alert");
String name = pushData.getString("name");
String contactNumber = pushData.getString("contactNumber");
String durationText = pushData.getString("duration");

mainIntent.putExtra("alert", alert);
mainIntent.putExtra("pushData", new PushData(name, contactNumber, durationText));

} catch (JSONException e) {
Log.i(TAG,"JSONException: " + e.getLocalizedMessage());
}

context.startActivity(mainIntent);

}
}

PushData.java

package com.appName;


import android.os.Parcel;
import android.os.Parcelable;


public class PushData implements Parcelable{
String name;
String contactNumber;
String durationText;


public PushData (String name,String contactNumber,String durationText ){
/*
* Reconstruct from the Parcel. Keep same order as in writeToParcel()
*/

this.name = name;
this.contactNumber = contactNumber;
this.durationText = durationText;
}

public PushData(Parcel source) {
name = source.readString();
contactNumber = source.readString();
durationText = source.readString();

}


@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(contactNumber);
dest.writeString(durationText);
}

@Override
public int describeContents() {
return 0;
}

public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {

public PushData createFromParcel(Parcel source) {

return new PushData(source);
}

public PushData[] newArray(int size) {

return new PushData[size];
}

};
}

MainActivity中onNewIntent的实现:

 @Override
protected void onNewIntent(Intent intent){
super.onNewIntent(intent);
setIntent(intent);

if (intent.getStringExtra("method").equals("updateStatus")) {
alertString = intent.getStringExtra("alert");
pushData = intent.getParcelableExtra("pushData");
updateStatus();
}
}



public void updateStatus() {


if (pushData != null) {
SharedPreferences.Editor editor = preferences.edit();
editor.putString("name", pushData.name);
editor.putString("contactNumber", pushData.contactNumber);
editor.putString("duration", pushData.durationText);
}
}

我可以知道这里出了什么问题吗?我在另一个应用程序上做了类似的实现,它工作得很好。

最佳答案

将 FLAG_ACTIVITY_SINGLE_TOP 标志添加到您的 Intent 中(参见onNewIntent(Intent intent))。

关于java - onNewIntent 没有被调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33967068/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com