Can anyone see why this Intent isnt being picked up? - android

//Create intent
notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
note = new Notification(android.R.drawable.btn_star_big_on, contentText, System.currentTimeMillis() );
Intent notificationIntent = new Intent();
notificationIntent.putExtra("aff_id",aff_id);
notificationIntent.setAction("com.mindfsck.PossAff.aff");
notificationIntent = notificationIntent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
contentIntent = PendingIntent.getActivity(ctx, aff_id, notificationIntent, 0);
note.setLatestEventInfo(ctx, title, aff_id + contentText, contentIntent);
notificationManager.notify(aff_id,note);
//Pickup intent
package com.mindfsck.PossAff;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.app.PendingIntent;
public class PosAffIntentReceiver extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.mindfsck.PossAff.aff")) {
System.out.println("Picked up broadcast with aff");
context.startActivity(new Intent(context, com.mindfsck.PossAff.MainActivity.class));
}else if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
}
System.out.println("Picked up broadcast");
}
}
//Manifest
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:debuggable="true">
<activity android:name=".MainActivity"
android:label="#string/app_name" android:configChanges="orientation|keyboardHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".PosAffIntentReceiver">
<intent-filter>
<!--<action android:name="com.mindfsck.PossAff.intent.action.aff"></action> -->
<action android:name="com.mindfsck.PossAff.aff"></action>
</intent-filter>
</receiver>
<receiver android:name=".NotificationAlarm">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</receiver>
</application>
I see the following in the Debug monitor when I click the notification
03-04 11:00:24.652: INFO/ActivityManager(1296): Starting activity: Intent { act=com.mindfsck.PossAff.aff flg=0x200000 (has extras) }
but it never gets picked up

Missing the DEFAULT category in the intent filter in your manifest? (Assuming I understand what you mean with "never gets picked up" correctly).

I changed:
contentIntent = PendingIntent.getActivity(ctx, aff_id, notificationIntent, 0);
To
contentIntent = PendingIntent.getBroadcast(ctx, aff_id, notificationIntent, 0);

Related

Getting NULL in context while auto restart with BROADCAST RECEIVER in Android app

I am developing android application which need auto restart when restart device for that I am using broadcast receiver with action BOOT_COMPLETED.
Broadcast receiver is receiving message when I am restarting device but in restart method I want to start main activity for that I used Intent but in onReceive method of receiver I am getting null context so I am unable to restart main Activity.
Below is code for that.
MainActivity.java
private Object activity;
private TextView tvImeiNum;
private BroadcastReceiver rebootreceiver;
private IntentFilter filter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvImeiNum = (TextView)findViewById(R.id.tv_imeinum);
filter = new IntentFilter();
filter.addAction(Intent.ACTION_BOOT_COMPLETED);
filter.addAction(Intent.ACTION_REBOOT);
rebootreceiver = new BootUpReceiver(MainActivity.this);
LocalBroadcastManager.getInstance(this).registerReceiver(rebootreceiver,
filter);//registering receiver
generateUniqueCode();
}
BootupReceiver.java
public class BootUpReceiver extends BroadcastReceiver {
MainActivity ma;
public BootUpReceiver(MainActivity maContext){
ma=maContext;
}
public BootUpReceiver(){
}
#RequiresApi(api = Build.VERSION_CODES.M)
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")) {
Log.d("TAG REBOOT", "onReceive: " + intent);
Log.d("Reboot complete", "connection");
GlobalTool.restartApplication(context);
}
}
}
GlobalTool.java
public class GlobalTool {
#RequiresApi(api = Build.VERSION_CODES.M)
public static void restartApplication(Context context) {
Log.d("IN App restart:", "");
Log.d("TAG", "restartApplication: ");
if(context != null)
{
Log.d("TAG NULL", "restartApplication: ");
Intent mainIntent = new Intent(context, MainActivity.class);
AlarmManager alarmMgr = (AlarmManager)
context.getSystemService(ALARM_SERVICE);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(mainIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
long alarmTime = System.currentTimeMillis() + (1 * 1000);
alarmMgr.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
Log.d("TAG", "restartApplication: 111");
Log.d("TAG", "restartApplication: 111");
}
else {
Intent mainIntent = new Intent(context, MainActivity.class);
AlarmManager alarmMgr = (AlarmManager)
context.getSystemService(ALARM_SERVICE);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addNextIntent(mainIntent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0,
PendingIntent.FLAG_UPDATE_CURRENT);
long alarmTime = System.currentTimeMillis() + (1 * 1000);
alarmMgr.setExact(AlarmManager.RTC_WAKEUP, alarmTime, pendingIntent);
Log.d("TAG", "restartApplication: 111");
Log.d("TAG", "restartApplication: 111");
}
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.kioskappdemo">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission
android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:name="KioskApplication"
android:allowBackup="true"
android:dataExtractionRules="#xml/data_extraction_rules"
android:fullBackupContent="#xml/backup_rules"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.KioskAppDemo"
tools:targetApi="31"
tools:ignore="Instantiatable">
<service
android:name=".RebootService"
android:enabled="true"
android:exported="true"></service>
<activity
android:name=".MainActivity"
android:exported="true"
android:screenOrientation="landscape"
android:theme="#style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".Activity.BootUpReceiver"
android:enabled="true"
android:exported="true">
<intent-filter >
<action
android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.REBOOT" />
<action
android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="com.reboot.test" />
<action
android:name="android.intent.action.TIMEZONE_CHANGED" />
<action
android:name="android.intent.action.DATE_CHANGED" />
</intent-filter>
</receiver>
</application>
</manifest>
Some you code improvements:
Don't put context to Brodcast Receiver constructor. Use the Context from onReceive method
public class BootUpReceiver extends BroadcastReceiver {
public BootUpReceiver(){
//TODO log
}
...
You should not use LocalBroadcastManager::registerReceiver call. If Broadcast receiver is added to AndroidManifest then it will be started by the system.
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<receiver
android:name=".activity.BootUpReceiver"
android:enabled="true"
android:exported="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter >
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
Don't use CamelCase for package names (use .activity.BootUpReceiver insetad .Activity.BootUpReceiver)

Android - alarm notification issue

I'm trying to configure an alarm notification in Android and unfortunately no alarm is generated.
Here is the creation of the alarm:
myDate = new SimpleDateFormat("dd/MM/yyyy HH:mm").parse(time_Date_str);
t.setDueDate(myDate);
t.setHasDate(true);
Intent alarmNotificationIntent = new Intent(this, ReminderNotification.class);
alarmNotificationIntent.putExtra("task", t);
PendingIntent pendingIntent =
PendingIntent.getBroadcast(this, (int) task_id, alarmNotificationIntent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(t.getDueDate().getTime());
alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pendingIntent);
In addition I've created:
NotificationReceiver
import android.app.Activity;
import android.os.Bundle;
public class NotificationReceiver extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
}
ReminderNotification
import android.content.Context;
import android.content.Intent;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
public class ReminderNotification extends BroadcastReceiver
{
public ReminderNotification()
{
// TODO Auto-generated constructor stub
}
#Override
public void onReceive(Context context, Intent intent) {
// The PendingIntent to launch our activity if the user selects this notification
Task task = (Task)intent.getSerializableExtra("task");
PendingIntent contentIntent = PendingIntent.getActivity(context, 0, new Intent(context, MainActivity.class), 0);
// Set the info for the views that show in the notification panel.
Intent snzInt = new Intent(context, SnoozeReminderReceiver.class);
snzInt.putExtra("task", task);
PendingIntent snoozeIntent = PendingIntent.getBroadcast(context, 0,snzInt, PendingIntent.FLAG_CANCEL_CURRENT);
Intent doneInt = new Intent(context,DoneActionReceiver.class);
doneInt.putExtra("task", task);
PendingIntent doneIntent = PendingIntent.getBroadcast(context, 0,doneInt, PendingIntent.FLAG_CANCEL_CURRENT);
NotificationManager notificationManager =
(NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
}
}
The manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="il.ac.she.dd.todoli"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<receiver android:name="ReminderNotification"/>
<receiver android:name="SnoozeReminderReceiver"/>
<receiver android:name="DoneActionReceiver"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".ListNodeActivity"
android:label="#string/title_activity_list_node"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme.NoActionBar">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="il.ac.shenkar.david.todolistex2.MainActivity" />
</activity>
<activity
android:name=".Signup_Activity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar"></activity>
<activity
android:name=".create_team"
android:label="Create new task team"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".invite_member"
android:label="Invite Members Your Team"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Login_activity"
android:label="Login to Wiggle"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
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=".EditTaskActivity"
android:label="Edit Task"
android:theme="#style/AppTheme.NoActionBar"></activity>
</application>
I have a feeling that I'm missing something very small but cannot find the issue.
Please let me know what I'm doing wrong.
Thanks in advance.
you have to first build notification and then notify it...
NotificationManager _manager =(NotificationManager)getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this)
.setContentTitle("New")
.setContentText("hello")
.setSmallIcon(R.drawable.ic_launcher)
.setContentIntent(pintent)
.build();
notification.vibrate=new long[]{100,250,100,500};
_manager.notify(notification_id, notification);
You can find a complete example of Alarm and Notification in my this GitHub Repo

Broadcast Receiver not working for me

I have the AlarmMainActivity which broadcasts an intent at a time set by the alarm. And the receiver program AlarmReceiver should catch this intent and send a notification. From the log, I can see that the alarm is being set, but the receiver doesn't start and its not working. Can you look at my code below and please let me know why the BroadcastReceiver is not working. Thanks.
AlarmMainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm_main);
Intent alertIntent = new Intent(this, AlarmReceiver.class);
final PendingIntent pendingIntent =
PendingIntent.getBroadcast
(this,1,alertIntent,PendingIntent.FLAG_UPDATE_CURRENT);
final Button alarmButton = (Button)findViewById(R.id.alarm_button);
alarmButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Long alertTime = new
GregorianCalendar().getTimeInMillis()+5*1000;
Log.i(TAG,"Alarm will be sent at : "+ alertTime.toString());
AlarmManager am =
(AlarmManager)getSystemService(Context.ALARM_SERVICE);
am.set(AlarmManager.RTC_WAKEUP,alertTime,pendingIntent );
Log.i(TAG, "Alarm is now set");
}
});
}//oncreate
AlarmReceiver:
public class AlarmReceiver extends BroadcastReceiver{
private static final String TAG = "AlarmReceiver";
private String msgTitle, msgText, msgTicker;
#Override
public void onReceive(Context context, Intent intent) {
Log.i(TAG, "Entered AlarmReceiver()");
msgTitle = "Todays Weather";
msgText = "Its Sunny and Warm";
msgTicker="Alert";
PendingIntent notifyIntent = PendingIntent.getActivity(context,0, new
Intent(context, AlarmMainActivity.class),0);
NotificationCompat.Builder mBuilder = new
NotificationCompat.Builder(context).
setSmallIcon(R.drawable.weather_image).
setTicker(msgTicker).
setContentTitle(msgTitle).
setContentText(msgText).
setDefaults(NotificationCompat.DEFAULT_SOUND).
setAutoCancel(true).
setContentIntent(notifyIntent);
NotificationManager notificationManager =
(NotificationManager)context.getSystemService
(Context.NOTIFICATION_SERVICE);
notificationManager.notify(1,mBuilder.build());
Log.i(TAG,"Notification Sent");
}
AndroidManifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.thirdlaw.alertalarm" >
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:debuggable="true">
<activity
android:name=".AlarmMainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<receiver android:name="com.thirdlaw.alertalarm.AlarmReceiver">
<intent-filter>
<action android:name="MY_ACTION_STRING"/>
</intent-filter>
</receiver>
</activity>
</application>
</manifest>
Remove the <receiver /> tag out of the <activity /> tag like :
<activity
android:name=".AlarmMainActivity"
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="com.thirdlaw.alertalarm.AlarmReceiver">
<intent-filter>
<action android:name="MY_ACTION_STRING"/>
</intent-filter>
</receiver>

Android AlarmManager doesn't work

I searched on the forum but it works for others. What is wrong in my AlarmManager?
I want to call the CallDataSend class in every minute
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent alarmIntent = new Intent(this, CallDataSend.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,
alarmIntent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (2 * 1000), pendingIntent);
Toast.makeText(this, "Alarm set", Toast.LENGTH_LONG).show();
}
}
the CallDataSend class:
public final class CallDataSend extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Alarm worked.", Toast.LENGTH_LONG).show();
}
}
and xml file:
<receiver
android:name="CallDataSend"
android:enabled="false">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
</intent-filter>
</receiver>
I believe you should make the receiver enabled before it can be instantiated by the system.
<receiver
android:name="CallDataSend"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
</intent-filter>
</receiver>
You also have to name your receiver with ".YourClass" instead of "YourClass" if that class is in the same package declared in your manifest's attributes or with "YourCompletePackage.YourClass" if it is not.
Ex:
in your case,
<receiver
android:name=".CallDataSend"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
</intent-filter>
</receiver>
in another case,
<receiver
android:name="com.example.CallDataSend"
android:enabled="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"></action>
</intent-filter>
</receiver>

clicking on notification to launch activity (email) app fails

The purpose of my app is to send an email at a predetermined time based on a schedule set with a broadcast receiver alarm using the users built in email configuration. The To: and Subject are already filled out. All the user has to do, is click on the notification when the alarm goes off, and it should prompt the user via an intent what email program to use via my SendMail.class.
If I call the class directly by dropping it into my "MainActivity onCreate it works. If I place it in my Intent definition where I build my notification it never appears to get called. This is the meat of my problem. I need the intent create chooser to pop up with its list of valid email programs when the user clicks on the notification and its just not working. Any help would be greatly appreciated! :)
Coincidentally, I also have a preferences fragment to hold the settings of my app, and if I replace the call to SendMail.class to SetPreferenceActivity, then the notification works as expected (i.e. it successfully calls another class when the user clicks on it).
Here is the code for my notification manager method:
public void createNotification(View view) {
Context context = getApplicationContext();
SharedPreferences mySharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String default_subject = mySharedPreferences.getString("default_subject", "");
default_subject = default_subject + android.text.format.DateFormat.format("E MM-dd", new java.util.Date());
Intent intent = new Intent(this, SendMail.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pIntent = PendingIntent.getActivity(this, 0, intent,
PendingIntent.FLAG_CANCEL_CURRENT);
Notification noti = new Notification.Builder(this)
.setContentTitle("Time to Send the Email")
.setContentText("Date: "+default_subject)
.setContentIntent(pIntent)
.build();
NotificationManager notificationManager = (NotificationManager) context
.getSystemService(Context.NOTIFICATION_SERVICE);
noti.flags |= Notification.FLAG_AUTO_CANCEL;
notificationManager.notify(0, noti);
}
And here is what is going on in the SendMail.class
public class SendMail extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sendMail();
}
public void sendMail() {
Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
"mailto", "somebody#gmail.com", null));
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Some Subject Text");
startActivity(Intent.createChooser(emailIntent, "Send email..."));
}
}
Here is the contents of my Manifest
<.....snipped header>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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>
<activity
android:name=".SetPreferenceActivity"
android:label="#string/app_name" >
</activity>
<receiver
android:name=".AlarmReceiver"
android:enabled="true"
android:exported="true"
android:label="MintBootReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.ACTION_BOOT_COMPLETED" />
<action android:name="android.intent.action.BOOT_COMPLETED" >
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
</action>
</intent-filter>
</receiver>
</application>

Categories

Resources