我尝试使用Intent将推送数据从接收器传递到活动,但它似乎无法正常工作。知道为什么吗?这是我的实施:
的AndroidManifest.xml
unsigned int num=0;
double percentage=0.00;
for(register unsigned int row=0;row<data_labels.label.size();row++)
{
if((data_labels.label[row]=correct_data_labels.label[row]))
{
num=num+1;
}
}
percentage = (num/data_labels.label.size());
<?xml version="1.0" encoding="UTF-8"?><!--
ParseReceiver
<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>
PushData.java
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);
}
}
在MainActivity中实现onNewIntent:
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];
}
};
}
我可以知道这里有什么问题吗?我在另一个应用程序上做了类似的实现,它完全正常。
答案 0 :(得分:1)
将FLAG_ACTIVITY_SINGLE_TOP标记添加到您的意图中 (见onNewIntent(Intent intent))。
答案 1 :(得分:0)
我认为它是s not about your FLAG,but your activity
的launchmode;
似乎只有当它的launchmode是singleTask或sigleTop时才会调用此方法;
答案 2 :(得分:0)
你的Manifest中也有拼写错误。变化
<activity android:name="com.appNamw.RouteView"
android:label="@string/title_activity_route_view"
android:screenOrientation="landscape">
要
<activity android:name="com.appName.RouteView"
android:label="@string/title_activity_route_view"
android:screenOrientation="landscape">