I want to create an alarm to create a notification.
The app that will create to Intent is not the same that will receive it.
I have that code, but it doesn't work, and I can't figure why :
First app:
package io.github.alucas.alarmsend;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "Create alarm", Toast.LENGTH_SHORT).show();
final Intent intent = new Intent("io.github.alucas.alarmreceive.ALARM");
PendingIntent alarmIntent = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + 1000 * 5, alarmIntent);
Toast.makeText(this, "Send alarm", Toast.LENGTH_SHORT).show();
}
}
Second app :
package io.github.alucas.alarmreceive;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "Receive alarm", Toast.LENGTH_SHORT).show();
}
}
Manifest :
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="io.github.alucas.alarmreceive">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<receiver
android:name="io.github.alucas.alarmreceive.AlarmReceiver"
android:enabled="true">
<intent-filter>
<action android:name="io.github.alucas.alarmreceive.ALARM"/>
</intent-filter>
</receiver>
</application>
</manifest>
[edit1]
Adding the flowing line fixed my problem :
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
Your second application has no activity. If you add an activity to your second app, install and run on the device, your code will work and the alarm will be delivered to the second application.
Adding the folowing line solved my problem :
intent.addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES);
Related
Activities:
MainActivity (Splash Screen)
MainActivity2 (Login)
SignupActivity (Signup)
MainActivity3 (Registering details)
ChatActivity (Hide on quiting from here)
Basically what i am trying to accomplish i i wanna hide my app after i do the initial signup part which will be handled uptill MainActivity3 and then when the user quits from the app the app icon should disappear and must only appear when called from dialer. My BroadcastReciever class never gets triggered,I am unable to figure out where i am going wrong Thanks in advance.
BroadCastReceiver.class
package com.insignia.socialmediasim;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.util.Log;
public class MyBroadCastReciever extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String ourCode = "**1234";
String dialedNumber = getResultData();
Log.d("triggered", "onReceive: "+dialedNumber);
if ((dialedNumber.equals(ourCode))){
// My app will bring up, so cancel the dialer broadcast
setResultData(null);
PackageManager packageManager = context.getPackageManager();
ComponentName componentName = new ComponentName(context, com.insignia.socialmediasim.MainActivity.class);
packageManager.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
//Intent to launch MainActivity
Intent intent_to_mainActivity = new Intent(context, MainActivity.class);
context.startActivity(intent_to_mainActivity);
}
}}
ChatActivity.class, "This is the class in which i hide my app"
package com.insignia.socialmediasim;
import androidx.appcompat.app.AppCompatActivity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
public class ChatActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_chat);
Intent intent = getIntent();
Toast.makeText(ChatActivity.this, "Welcome Back to the adobe " + intent.getStringExtra("type"), Toast.LENGTH_LONG).show();
}
#Override
protected void onStop() {
super.onStop();
PackageManager packageManager = getPackageManager();
ComponentName componentName = new ComponentName(ChatActivity.this, com.insignia.socialmediasim.MainActivity.class);
packageManager.setComponentEnabledSetting(componentName,PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}}
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.insignia.socialmediasim">
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<application
android:allowBackup="true"
android:icon="#mipmap/asd"
android:label="#string/appnameda"
android:roundIcon="#mipmap/asd"
android:supportsRtl="true"
android:theme="#style/Theme.Design.NoActionBar">
<activity android:name=".ChatActivity" />
<activity android:name=".MainActivity3" />
<activity android:name=".SignupActivity" />
<activity android:name=".MainActivity2" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name=".MyBroadCastReciever">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
It is recommended that register and unregister the broadcast programmatically in you activity.Try It.
this is a sample code for registering a broadcastReceiver:
IntentFilter filter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
filter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
this.registerReceiver(br, filter);
You can check more information from this document: broadcasts
I am simply trying to implement the AlarmManager. I wrote the code for alarm manager but the code doesnot work. AlarmManager doesnot fire the Broadcast Receiver and service. But when I donot use the AlarmManager and simply start the service using intent the service run. How to make AlarmManager work to schedule the service periodically?
Below is the code:
MainActivity.java
package com.alarmmanager;
import android.app.*;
import android.os.Bundle;
import android.content.*;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent intent = new Intent(this,AlarmReceiver.class);
PendingIntent pending =PendingIntent.getService(this, 0, intent, 0);
AlarmManager alarm =(AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
alarm.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis(),15000,pending);
}
}
AlarmReceiver.java
package com.alarmmanager;
import android.content.*;
public class AlarmReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
intent =new Intent(context,MainService.class);
context.startService(intent);
}
}
MainService.java
package com.alarmmanager;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class MainService extends Service
{
public IBinder onBind(Intent intent) {
return null;
}
public void onCreate() {
Log.i("nilavs","nilav");
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.alarmmanager"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="23" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM"/>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<service
android:name=".MainService"
android:enabled="true"
/>
<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:process=":remote" android:name=".AlarmReceiver">
</receiver>
</application>
</manifest>
You should use getBroadcast instead of getService?
PendingIntent pending =PendingIntent.getService(this, 0, intent, 0);
Edit:
Try setting it to fire off in the future initially - e.g.
alarm.setRepeating(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+5000,15000,pending);
I need to start an app when the [Android] phone starts.
I compiled this code, and the app doesn't crash but doesn't show me anything either!
Now I'm trying with Toast but it still isn't found.
Can someone help me?
This is the main activity:
package com.example.simplenotification;
import android.app.Activity;
import android.app.AlarmManager;
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.support.v4.app.NotificationCompat;
import android.support.v4.app.NotificationManagerCompat;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RemoteViews;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final int MY_NOTIFICATION_ID = 0;
private int mNotificationCount;
private final CharSequence tickerText ="this is tickerText";
private final CharSequence contentTitle="this contentTitle";
private final CharSequence contentText="this coontentText";
private Intent mNotificationIntent;
private PendingIntent mContentIntent;
RemoteViews mContentView = new RemoteViews("com.example.simplenotification.StatusBarWithCustomView", R.layout.custom_view);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNotificationIntent = new Intent(getApplicationContext(),AppGet.class);
mContentIntent = PendingIntent.getActivity(getApplicationContext(), 0, mNotificationIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v){
startNotification();
}
});
}
public void startNotification(){
Intent notificationIntent = new Intent(getApplicationContext(), MainActivity.class);
// set intent so it does not start a new activity
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
PendingIntent intent =
PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(getApplicationContext())
.setTicker(tickerText)
.setSmallIcon(R.drawable.ic_launcher)
.setAutoCancel(true)
.setContentTitle("titolo content")
.setContentText("content text")
.setContentIntent(intent);
NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(getApplicationContext());
mNotificationManager.notify(MY_NOTIFICATION_ID,notificationBuilder.build());
}
}
This the Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.simplenotification"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="14" />
<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>
<receiver android:enabled="true" android:name=".BootUpReceiver"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
</application>
I don't know how debug the app autorun on emulator. I'm sorry
EDIT: I put in another class the reciever and changed the 'android name'
package com.example.simplenotification;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;
public class BootUpReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(Intent.ACTION_BOOT_COMPLETED)) {
Intent i = new Intent();
i.setAction("android.intent.action.MAIN");
context.startService(i);
Toast.makeText(context , "saranno 3?" , Toast.LENGTH_SHORT).show();
}
}
}
Not 100% sure, but try changing this
Toast.makeText(context , "saranno 3?" , Toast.LENGTH_SHORT).show();
into
Toast.makeText(context.getApplicationContext() , "saranno 3?" , Toast.LENGTH_SHORT).show();
Make a separate class for BootUpReceiver. And in manifest pass correct path of ur package name in android:name while defining receiver
<receiver android:name=".receivers.BootUpReceiver"
Update->
Also use this permission in your manisfest
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
I am creating an app with will run everyday morning onetime. I am getting RunTimeException error so unable to understand, if the app is really running or not?
error is: Unable to instantiate receiver. even if I declared receiver in my manifest
MainSchedulerClass.java
package com.example.displayscheduler;
import java.util.Calendar;
import java.util.TimeZone;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
public class MyScheduler extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.schedule);
setRecurringAlarm(this);
}
private void setRecurringAlarm(Context context) {
Calendar updateTime = Calendar.getInstance();
updateTime.setTimeZone(TimeZone.getTimeZone("GMT+5:00"));
updateTime.set(Calendar.HOUR_OF_DAY, 10);
updateTime.set(Calendar.MINUTE, 00);
Intent intent = new Intent(context, Tasks.class);
PendingIntent recurringDownload = PendingIntent.getBroadcast(context,
0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager alarms = (AlarmManager) this.getSystemService(
Context.ALARM_SERVICE);
alarms.setInexactRepeating(AlarmManager.RTC_WAKEUP,
updateTime.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, recurringDownload);
}
}
Tasks.java
package com.example.displayscheduler;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
public class Tasks extends BroadcastReceiver {
final public static String ONE_TIME = "onetime";
#Override
public void onReceive(Context context, Intent intent) {
Log.i(ONE_TIME, "Executed Tasks.Java File");
//Some task here for every morning
Toast.makeText(context, "Start Displaying Pictures", Toast.LENGTH_LONG).show();
}
}
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.displayscheduler"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.VIBRATE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MyScheduler"
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=".Tasks"
android:enabled="true"
android:label="#string/app_name" >
</activity>
<receiver android:name="com.example.displayscheduler.MyScheduler" >
</receiver>
</application>
</manifest>
You wrote activity name in receiver tag, change it to Tasks i.e your BroadcastReceiver class.
Replace this:
<receiver android:name="com.example.displayscheduler.MyScheduler" >
</receiver>
to this:
<receiver android:name="com.example.displayscheduler.Tasks" >
</receiver>
in your AndroidManifest.xml
MyScheduler class is not broadcast receiver you must change it with task in manifest.xml
<receiver android:name="com.example.displayscheduler.Task" >
</receiver>
Make sure that the java file name is same as your receiver name.
Rename your java file Tasks.java to MyScheduler.java
refer this sample https://developer.android.com/training/scheduling/alarms.html
I'm trying to set an alarm at boot time, and have it call an IntentService periodically via the AlarmManager. The AlarmReceiver is never triggered, not sure why.
BootReceiver:
package com.company.android;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.text.format.Time;
public class BootBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Time now = new Time();
now.setToNow();
Log.e("#######################COMM", "Booting up " + now.toString());
Intent alarmIntent = new Intent("com.company.android.AlarmReceiver");
PendingIntent pi = PendingIntent.getBroadcast(context, 0, alarmIntent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, System.currentTimeMillis(), 1000 * 5, pi);
Log.e("#######################COMM", "Booted up " + now.toString());
}
}
AlarmReceiver:
package com.company.android;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.widget.Toast;
/**
* Created by mlaino on 7/8/13.
*/
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.d("COMM", "Pre");
Intent i = new Intent("com.company.android.PollingService");
context.startService(i);
Log.d("COMM","Pos");
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company.android" android:versionCode="1" android:versionName="1.0-SNAPSHOT">
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="15" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<receiver android:name=".BootBroadcastReceiver"
android:enabled="true"
android:permission="android.permission.RECEIVE_BOOT_COMPLETED" >
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</receiver>
<receiver android:name=".AlarmReceiver">
<intent-filter>
<action android:name="com.company.android.AlarmReceiver" />
</intent-filter>
</receiver>
<activity android:name=".CommunicationsManagerActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".PollingService" >
<intent-filter>
<action android:name="com.company.android.PollingService" />
</intent-filter>
</service>
</application>
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
</manifest>
Any clues?
Thanks
First, remove android:permission="android.permission.RECEIVE_BOOT_COMPLETED", as you are saying that whatever is calling your receiver must hold that permission, which may or may not be the case.
Then, be sure to run one of your activities before rebooting, as manifest-registered BraodcastReceivers will not work until something explicitly runs one of your components, typically accomplished by the user launching an activity.