我正在尝试学习android所以我正在制作一些应用程序来玩它来使用它。我现在正在做一个问题,我正在尝试制作一个短信通知应用程序,但它总是在模拟器中强行关闭。这是我的代码
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
public class Sms extends BroadcastReceiver {
static final String ACTION = "android.provider.Telephony.SMS_RECEIVED";
@Override
public void onReceive(Context arg0, Intent arg1) {
NotificationManager mNotificationManager = (NotificationManager) arg0.getSystemService(Context.NOTIFICATION_SERVICE);
if (arg1.getAction().equals(ACTION)) {
StringBuilder sb = new StringBuilder();
String from = new String();
String body = new String();
Bundle bundle = arg1.getExtras();
if (bundle != null) {
Object[] pdus = (Object[]) bundle.get("pdus");
for (Object pdu : pdus){
SmsMessage messages = SmsMessage.createFromPdu((byte[]) pdu);
sb.append(messages.getDisplayOriginatingAddress());
from = messages.getDisplayOriginatingAddress();
sb.append(messages.getDisplayMessageBody());
body= messages.getDisplayMessageBody();
//Log.i(LOG_TAG, "[SMSApp] onReceiveIntent: " + sb);
//abortBroadcast();
}// end for
}//end if
int icon = R.drawable.icon;
CharSequence tickerText = from + ": " + body;
long when = System.currentTimeMillis();
Notification notification = new Notification(icon, tickerText, when);
CharSequence contentTitle = "New SMS Message";
CharSequence contentText = sb.toString();
Intent notificationIntent = new Intent();
PendingIntent contentIntent = PendingIntent.getActivity(arg0, 0, notificationIntent, 0);
notification.setLatestEventInfo(arg0, contentTitle, contentText, contentIntent);
notification.vibrate = new long[] { 100, 250, 100, 500};
notification.flags |= Notification.FLAG_AUTO_CANCEL;
mNotificationManager.notify(1, notification);
}//end if
}
}
这是我的manifest.xml文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.froyo"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".FroyoTest"
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=".Sms">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.WRITE_SMS" />
<uses-permission android:name="android.permission.SEND_SMS" />
</manifest>
我不明白什么是错的,因为它每次强制关闭
这是logcat错误
02-02 11:35:46.896: ERROR/System(66): Failure starting core service
02-02 11:35:46.896: ERROR/System(66): java.lang.SecurityException
02-02 11:35:46.896: ERROR/System(66): at android.os.BinderProxy.transact(Native Method)
02-02 11:35:46.896: ERROR/System(66): at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
02-02 11:35:46.896: ERROR/System(66): at android.os.ServiceManager.addService(ServiceManager.java:72)
02-02 11:35:46.896: ERROR/System(66): at com.android.server.ServerThread.run(SystemServer.java:184)
02-02 11:36:09.847: ERROR/ActivityManager(66): ANR in com.android.settings
02-02 11:36:09.847: ERROR/ActivityManager(66): Reason: Broadcast of Intent { act=android.intent.action.CLOSE_SYSTEM_DIALOGS cmp=com.android.settings/.widget.SettingsAppWidgetProvider (has extras) }
02-02 11:36:09.847: ERROR/ActivityManager(66): Load: 2.95 / 0.74 / 0.24
02-02 11:36:09.847: ERROR/ActivityManager(66): CPU usage from 7048ms to 569ms ago:
02-02 11:36:09.847: ERROR/ActivityManager(66): app_process: 52% = 39% user + 13% kernel / faults: 2461 minor
02-02 11:36:09.847: ERROR/ActivityManager(66): system_server: 46% = 28% user + 17% kernel / faults: 704 minor 3 major
02-02 11:36:09.847: ERROR/ActivityManager(66): ronsoft.openwnn: 40% = 25% user + 15% kernel / faults: 3986 minor 6 major
02-02 11:36:09.847: ERROR/ActivityManager(66): ndroid.launcher: 26% = 14% user + 11% kernel / faults: 3701 minor 3 major
02-02 11:36:09.847: ERROR/ActivityManager(66): m.android.phone: 25% = 13% user + 12% kernel / faults: 3322 minor 7 major
02-02 11:36:09.847: ERROR/ActivityManager(66): ndroid.settings: 19% = 9% user + 10% kernel / faults: 3007 minor 7 major
02-02 11:36:09.847: ERROR/ActivityManager(66): bootanimation: 10% = 5% user + 4% kernel
02-02 11:36:09.847: ERROR/ActivityManager(66): sh: 1% = 0% user + 0% kernel / faults: 118 minor
02-02 11:36:09.847: ERROR/ActivityManager(66): qemud: 0% = 0% user + 0% kernel
02-02 11:36:09.847: ERROR/ActivityManager(66): TOTAL: 100% = 65% user + 34% kernel
02-02 11:36:32.439: ERROR/HierarchicalStateMachine(66): TetherMaster - unhandledMessage: msg.what=3
02-02 11:37:48.610: ERROR/AndroidRuntime(278): FATAL EXCEPTION: main
02-02 11:37:48.610: ERROR/AndroidRuntime(278): java.lang.RuntimeException: Unable to start receiver com.example.froyo.Sms: java.lang.SecurityException: Requires VIBRATE permission
02-02 11:37:48.610: ERROR/AndroidRuntime(278): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2821)
02-02 11:37:48.610: ERROR/AndroidRuntime(278): at android.app.ActivityThread.access$3200(ActivityThread.java:125)
02-02 11:37:48.610: ERROR/AndroidRuntime(278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2083)
02-02 11:37:48.610: ERROR/AndroidRuntime(278): at android.os.Handler.dispatchMessage(Handler.java:99)
02-02 11:37:48.610: ERROR/AndroidRuntime(278): at android.os.Looper.loop(Looper.java:123)
02-02 11:37:48.610: ERROR/AndroidRuntime(278): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-02 11:37:48.610: ERROR/AndroidRuntime(278): at java.lang.reflect.Method.invokeNative(Native Method)
02-02 11:37:48.610: ERROR/AndroidRuntime(278): at java.lang.reflect.Method.invoke(Method.java:521)
02-02 11:37:48.610: ERROR/AndroidRuntime(278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-02 11:37:48.610: ERROR/AndroidRuntime(278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-02 11:37:48.610: ERROR/AndroidRuntime(278): at dalvik.system.NativeStart.main(Native Method)
02-02 11:37:48.610: ERROR/AndroidRuntime(278): Caused by: java.lang.SecurityException: Requires VIBRATE permission
02-02 11:37:48.610: ERROR/AndroidRuntime(278): at android.os.Parcel.readException(Parcel.java:1247)
02-02 11:37:48.610: ERROR/AndroidRuntime(278): at android.os.Parcel.readException(Parcel.java:1235)
02-02 11:37:48.610: ERROR/AndroidRuntime(278): at android.app.INotificationManager$Stub$Proxy.enqueueNotificationWithTag(INotificationManager.java:274)
02-02 11:37:48.610: ERROR/AndroidRuntime(278): at android.app.NotificationManager.notify(NotificationManager.java:110)
02-02 11:37:48.610: ERROR/AndroidRuntime(278): at android.app.NotificationManager.notify(NotificationManager.java:90)
02-02 11:37:48.610: ERROR/AndroidRuntime(278): at com.example.froyo.Sms.onReceive(Sms.java:51)
02-02 11:37:48.610: ERROR/AndroidRuntime(278): at android.app.ActivityThread.handleReceiver(ActivityThread.java:2810)
02-02 11:37:48.610: ERROR/AndroidRuntime(278): ... 10 more
02-02 11:37:54.500: ERROR/ContactsProvider(172): Cannot determine the default account for contacts compatibility
02-02 11:37:54.500: ERROR/ContactsProvider(172): android.accounts.AuthenticatorException: bind failure
02-02 11:37:54.500: ERROR/ContactsProvider(172): at android.accounts.AccountManager.convertErrorToException(AccountManager.java:1433)
02-02 11:37:54.500: ERROR/ContactsProvider(172): at android.accounts.AccountManager.access$400(AccountManager.java:134)
02-02 11:37:54.500: ERROR/ContactsProvider(172): at android.accounts.AccountManager$BaseFutureTask$Response.onError(AccountManager.java:1338)
02-02 11:37:54.500: ERROR/ContactsProvider(172): at android.accounts.IAccountManagerResponse$Stub.onTransact(IAccountManagerResponse.java:69)
02-02 11:37:54.500: ERROR/ContactsProvider(172): at android.os.Binder.execTransact(Binder.java:288)
02-02 11:37:54.500: ERROR/ContactsProvider(172): at dalvik.system.NativeStart.run(Native Method)
答案 0 :(得分:1)
添加振动权限修复它