我必须做一个实现推送通知的应用程序。我已经下载了google-play-services_lib并将其导入到我的项目中,但是,当我运行appm时,它给了我这个错误:
在附加的堆栈跟踪中获取了资源,但从未释放。有关避免资源泄漏的信息,请参阅java.io.Closeable。
然后它说:
无法执行索引:开销限制超出
我认为有一个问题与库(它的导入),因为代码是真正的样本,我不调用库的任何函数我怎样才能修好它?
这是代码:
public class MainActivity extends Activity {
public static final String EXTRA_MESSAGE = "message";
public static final String PROPERTY_REG_ID = "registration_id";
private static final String PROPERTY_APP_VERSION = "appVersion";
private static final int PLAY_SERVICES_RESOLUTION_REQUEST = 9000;
private Context context;
private String SENDER_ID = "xxxxxxxxxx";
/**
* Tag used on log messages.
*/
private static final String TAG = "GCM Demo";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
StrictMode.VmPolicy.Builder vmPolicyBuilder = new StrictMode.VmPolicy.Builder().detectAll().penaltyLog();
setContentView(R.layout.activity_main);
context = this.getApplicationContext();
}
private void registerCloudMessaging() {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
// repeated calls to this method will return the same registration ID
// a new registration is needed if the app is updated or backup & restore happens
try {
String registrationId = gcm.register(SENDER_ID);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// then uploads the registration ID to your server
}
@Override
protected void onDestroy() {
super.onDestroy();
}
}
public class BroadcastReceiver extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
GoogleCloudMessaging gcm = GoogleCloudMessaging.getInstance(context);
String messageType = gcm.getMessageType(intent);
if (GoogleCloudMessaging.MESSAGE_TYPE_SEND_ERROR.equals(messageType)) {
// error occurs
} else if (GoogleCloudMessaging.MESSAGE_TYPE_DELETED.equals(messageType)) {
// the server have deleted some pending messages,
// because they are collapsible
} else if (GoogleCloudMessaging.MESSAGE_TYPE_MESSAGE.equals(messageType)) {
// normal message
// how the data can be fetched is detailed in the next section
}
}
}
这是manifest。xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.provapush"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE"/>
<permission
android:name="com.example.provapush.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.provapush.permission.C2D_MESSAGE" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- Required for applications which use Google Play Services. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".BroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<!-- Receives the actual messages. -->
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.example.provapush" />
</intent-filter>
</receiver>
</application>
</manifest>
我使用的模拟器一个谷歌api级别18
以上代码在我的手机上运行良好。尝试在手机上运行代码,而不是在模拟器上运行。有时模拟器不能按预期工作。
你不需要在手机上安装谷歌api和播放服务,这可能会创建一些无法在模拟器上关闭的资源。