This is part of a code that I combine together from google and youtube, to execute "turnOnLed" when the alarm go off. For some reason it doesnt execute and I don't know how to make it work. Can someone help? I've Google it, but no succes.
Thank you.
private void startAlarm(Calendar c)
{
AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
if (c.before(Calendar.getInstance()));
{
c.add(Calendar.DATE, 1);
}
Intent intent = new Intent(this, Control.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,intent, 0);
alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(), pendingIntent);
}
public class Control extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent)
{
turnOnLed(); //method to turn on
}
}
And the Manifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name="art.control4.IntroScreen"
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="art.control4.DeviceList"
android:label="#string/app_name">
</activity>
<activity
android:name="art.control4.ledControl"
android:label="#string/app_name">
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
</activity>
<receiver android:name=".ledControl$Control"
android:process=":remote"/>
Related
I get this error when I'm trying to start an intent from my wear to my mobile. When I press a button on my watch there's a message send to the listenerservice. And I'm trying to start a method from my listenerService to my MainActivity but I'm getting the error:
java.lang.ClassCastException: android.app.Application cannot be cast to com.my_emergency.samdesmedt.my_emergency.MainActivity
at com.my_emergency.samdesmedt.my_emergency.ListenerServiceFromWear.onMessageReceived(ListenerServiceFromWear.java:28)
This is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my_emergency.samdesmedt.my_emergency">
<uses-permission android:name="android.permission.SEND_SMS" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<uses-permission android:name="android.permission.READ_SMS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
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>
<activity
android:name=".AddActivity">
</activity>
<activity
android:name=".SettingsActivity">
</activity>
<activity
android:name=".EditSettingsActivity">
</activity>
<activity
android:name=".EditActivity" >
</activity>
<service android:name=".ListenerServiceFromWear">
<intent-filter>
<action android:name="com.google.android.gms.wearable.BIND_LISTENER"/>
</intent-filter>
</service>
</application>
</manifest>
This is my ListenerServiceFromWear.Java
#Override
public void onMessageReceived(MessageEvent messageEvent) {
super.onMessageReceived(messageEvent);
if (messageEvent.getPath().equals(WEARPATH)) {
Intent intent = new Intent(this, MainActivity.class);
intent.putExtra("isEmergency", true);
startActivity(intent);
}
}
This is the code in my mainActivity
Intent intent = getIntent();
boolean isEmergency = intent.getBooleanExtra("isEmergency", false);
if(isEmergency){
startEmergencyMode(message);
}
Intent intent = new Intent(getBaseContext(), MainActivity.class);
fixed it
I want to get the called number in android but When I start the outgoing call it fails I am using broadcast receiver and register it in service to keep listen if activity not in focus here is my code.
Menifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vampirepc.androidservice" >
<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" />
<action android:name="android.intent.action.BOOT_COMPLETED">
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</action>
</intent-filter>
</activity>
<service android:name=".MyService" />
<receiver
android:name="com.example.vampirepc.androidservice.OutgoingReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
</application>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
</manifest>
BroadcastReceiver Class
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(ctx,
"Inside broadcast",
Toast.LENGTH_LONG).show();
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(context, "Outgoing call catched: " + phoneNumber, Toast.LENGTH_LONG).show();
}
Service
#Override
public void onCreate() {
Toast.makeText(getApplicationContext(),"Service created",Toast.LENGTH_SHORT).show();
try {
IntentFilter filter = new IntentFilter("android.intent.action.NEW_OUTGOING_CALL");
OutgoingReceiver myReceiver = new OutgoingReceiver();
registerReceiver(myReceiver, filter);
} catch (Exception e) {
Toast.makeText(getApplicationContext(),"Exception is "+String.valueOf(e),Toast.LENGTH_SHORT).show();
}
}
Have a look at this
Phone state Broadcaster
Please add the permission in the top ,this helped me
?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vampirepc.androidservice" >
android:name="android.permission.PROCESS_OUTGOING_CALLS" />
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<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" />
<action android:name="android.intent.action.BOOT_COMPLETED">
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</action>
</intent-filter>
</activity>
<receiver
android:name="com.example.vampirepc.androidservice.OutgoingReceiver"
android:enabled="true"
android:exported="true" >
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
<service android:name=".MyService" />
</application>
<uses-permission
</manifest>
Have you realized in this line:
Toast.makeText(ctx,
"Inside broadcast",
Toast.LENGTH_LONG).show();
You used ctx instead of context.
Thank to all i have solved the problem my self I was using incorrect context in toaster the correct onReceive is.
I was using ctx which was initialize.
Context ctx;
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, "this is not shown" , Toast.LENGTH_LONG).show();
String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Toast.makeText(context, "Outgoing call catched: " + phoneNumber, Toast.LENGTH_LONG).show();
}
I have made an application that requires alarms to be fired.Alarm Manager works fine when they are scheduled from some activity. Since alarms are deleted when reboot occurs,i have made a boot_Receiver to reschedule the alarms. But the problem is that alarms are not fired when they are scheduled from the bootreceiver class. Please Help..
AlarmManager Class:-
public class NotifyService extends WakefulBroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Notification_Holder notifholder;
Type t=new TypeToken<Notification_Holder>(){}.getType();
Gson js=new Gson();
notifholder=js.fromJson(intent.getStringExtra("one"),t);
Notification_Creator notifcreator=new Notification_Creator(notifholder.title,notifholder.content,notifholder.cal,context);
notifcreator.create_notification();
}
}
BootReceiver class:-
public class AtBoot extends BroadcastReceiver {
SharedPreferences sharedPreferences;
AlarmManager alarmManager;
Intent x;
PendingIntent pendingIntent;
#Override
public void onReceive(Context context, Intent intent) {
sharedPreferences = context.getSharedPreferences("todoshared", Context.MODE_PRIVATE);
String f = sharedPreferences.getString("todolist", null);
Gson json = new Gson();
alarmManager=(AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
vClass.notes = Notification_Holder.convert_from_jason(f);
x=new Intent(context,NotifyService.class);
pendingIntent=PendingIntent.getService(context,2100,x,0);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(),pendingIntent);
Vibrator vib=(Vibrator)context.getSystemService(Context.VIBRATOR_SERVICE);
if(vib.hasVibrator()) {
vib.vibrate(1500);
}
}
}
Manifest File:-
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.android.alarm.permission.SET_ALARM" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<application
android:allowBackup="true"
android:hardwareAccelerated="true"
android:label="#string/app_name"
android:supportsRtl="true"
android:icon="#mipmap/ic_launcher"
android:theme="#style/MyAppBar">
<activity
android:name=".scrapper"
android:noHistory="true"
android:theme="#style/AppTheme.NoActionBar"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".schedule"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".workSpace"
android:theme="#style/AppTheme.NoActionBar" />
<receiver android:name=".widget">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_info" />
</receiver>
<service
android:name=".widgetService"
android:permission="android.permission.BIND_REMOTEVIEWS" />
<receiver android:name=".NotifyService" />
<activity
android:name=".showSubject"
android:theme="#style/AppTheme.popupTheme" />
<activity android:name=".splashScreen"
android:theme="#style/AppTheme.NoActionBar"
android:noHistory="true"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver
android:name=".AtBoot"
android:enabled="true"
android:exported="true"
android:label="AtBoot">
<intent-filter>
<action android:name="android.intent.action.BOOT_COMPLETED" />
<action android:name="android.intent.action.QUICKBOOT_POWERON" />
</intent-filter>
</receiver>
</application>
I have added the vibrator in AtBoot class t check if it is being called on bootup. And the phone vibrates..This means that the class is being invoked on bootup but the alarm part does not work
I think for HTC you should also add in the IntentFilters
<action android:name="com.htc.intent.action.QUICKBOOT_POWERON"/>
Also, I think it should be
PendingIntent.getBroadcast(context, 2100, x, 0);
and not
PendingIntent.getService(context,2100,x,0);
The problem was that i was using pendingintent.getService() instead of pendingIntent.getBroadcast() as correctly pointed out by Velislava...
hi i have used the following code in receiver class
public class AlarmReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Log.e("the time is right","yay!");
Intent i = new Intent(context, AlarmServie.class);
context.startService(i);
}
}
here i the code which i used in service class
public class AlarmServie extends Service {
#Nullable
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Log.e("onStart:","came" );
/* NotificationManager notifyman = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
Intent main_activity = new Intent(this.getApplicationContext(), MainActivity.class);
PendingIntent o = PendingIntent.getActivity(this, 0, main_activity, 0);
/*Notification noti = new Notification.Builder(this)
.setContentTitle("Reminder to Pill")
.setContentText("Click for info")
.setAutoCancel(true)
.setContentIntent(o)
.build();*/
NotificationManager nm = (NotificationManager) this.getApplicationContext().getSystemService(NOTIFICATION_SERVICE);
Intent in = new Intent(this.getApplicationContext(), MainActivity.class);
PendingIntent pending = PendingIntent.getactivity(this, 0, in, 0);
NotificationCompat.Builder mBuilder =new NotificationCompat.Builder(AlarmServie.this);
mBuilder.setContentTitle("Pill Reminder");
mBuilder.setContentText("CLick here to View");
//mBuilder.setSound(sound);
TaskStackBuilder ts=TaskStackBuilder.create(this);
ts.addParentStack(MainActivity.class);
nm.notify(9999,mBuilder.build());
}}
i created this code to get notification.when i run the app the receiver class is triggered but it is not moving to service class to invoke notification.can anyone say whats wrong in the code or say me how to get notification using broadcast receiver and service in a detailed tutorial
here is the manifest file and say me where to put the code exactly
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>
<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:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
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>
<receiver android:name=".AlarmReceiver"></receiver>
<activity android:name=".Set"></activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
please explain in detail what the problem in code.please tell exactly where to put the service tag
You must declare service in manifest also.
Intent to start service is sent to system then system tries to find right application component to handle this particular intent using information passed in manifest files.
It should be enough, but I'm not sure if you put service in the default package.
<application>
....
<service android:name=".AlarmServie"/>
....
</application>
Your Manifest should look something like this
<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"
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>
//here is your service
<service android:name=".AlarmServie" android:enabled="true"/>
<receiver android:name=".AlarmReceiver"></receiver>
<activity android:name=".Set"></activity><!-- ATTENTION: This was auto-generated to add Google Play services to your project for
App Indexing. See https://g.co/AppIndexing/AndroidStudio for more information. -->
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
1.This is the class that sends broadcast to main Activity:
public class LecturaDatos extends Service {
inetnt = new Intent(this, MainActivity.class);
sendBroadcast(inetnt);
}
2.This is main Activity and receives the broadcast:
class xReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent1) {
Toast.makeText(context, "Intent Detected.", Toast.LENGTH_LONG).show();
System.out.println("received");
}
}
3.Finally this is my manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.haizea_android"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="5"
android:targetSdkVersion="5" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name="xReceiver">
<intent-filter>
<action android:name="com.javacodegeeks.android.A_CUSTOM_INTENT">
</action>
</intent-filter>
</receiver>
<service
android:name=".LecturaDatos"
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>
</application>
</manifest>
I don't know why but it doesn't work and I have seen similar problems and tried several solutions but they haven't worked for me
you should set action to your broadcasted intent e.g.
inetnt.setAction("com.javacodegeeks.android.A_CUSTOM_INTENT");