我正在通过此广播接收器发送通知,该报警器由警报管理员触发发送短信但我无法看到任何通知。
我还试图包含已发送的意图和交付意图,但不能在这里使用registerReciever。
这是我的代码。
广播接收器
public class MyReceiver extends BroadcastReceiver {
String SENT="sent";
public final String tag="com.example.pritesh.smstimer";
public MyReceiver() {
}
//@Override
public void onReceive(Context context, Intent intent) {
Log.i(tag,"Sending");
NotificationManager notificationManager= (NotificationManager)
context.getSystemService(Context.NOTIFICATION_SERVICE);
Intent intent1=new Intent(context,Time_Picker.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context,0,intent1,0);
Notification.Builder notification=new Notification.Builder(context)
.setContentTitle("SMS SENT")
.setContentText("Your SMS has Been Sent")
.setContentIntent(pendingIntent);
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(Time_Picker.Phone,null, Time_Picker.Message, null, null);
notificationManager.notify(0,notification.build());
Toast.makeText(context, "Sms Sent", Toast.LENGTH_LONG).show();
}
}
清单
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.SEND_SMS" />
<application
android:allowBackup="true"
android:icon="@drawable/favicon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".Time_Picker"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".noti"/>
<activity android:name=".MainActivity">
</activity>
<receiver
android:name=".MyReceiver"
android:enabled="true"
android:exported="true"
android:launchMode="singleTask"
android:taskAffinity=""
android:excludeFromRecents="true"/>
</application>
答案 0 :(得分:0)
您是否在androidmanifest.xml中注册了接收器或者调用了registerReceiver来执行相同操作?我不认为它会发挥作用。
见这里:https://developer.android.com/guide/topics/manifest/receiver-element.html