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();
}
Related
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...
I want to add a service to my manifest but it gives a warning in.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.aura_apps.tygo_asbroek.intentexample" >
<application
android:allowBackup="true"
android:icon="#mipmap/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=".SecondActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:name=".TygoIntentService"
>
</service>
</application>
Here is the Java code:
package com.aura_apps.tygo_asbroek.intentexample;
public class TygoIntentService extends IntentService {
private static final String TAG = "com.bluelionapps.intentexample";
public TygoIntentService(String name) {
super("TygoIntentService");
}
#Override
protected void onHandleIntent(Intent intent) {
//This is what the service does
Toast toast = Toast.makeText(getApplicationContext(), "Service Lauched", Toast.LENGTH_SHORT);
toast.show();
}
}
Here you see my complete Manifest but .TygoIntentService says TygoIntentService.java doesn't have a default constructor of my app. If i run it, it's just working but I want to know if this can give any problems in the future and if it gives one what i need to do.
Remove String name parameter from the constructor:
public TygoIntentService() {
super("TygoIntentService");
}
If you do not use/need the name String there, just remove the constructor, and let the compiler add it behind scenes by default.
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");
As I understood, if I define configuration activity for widget, onUpdate() method will not call, until I'll not configure it. But in my case, and I wonder why - configuration activity doesn't start, and update method calls after placing widget. My application have other activities, one of them starts when application run, maybe that's is the reason? Here my code:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.watcom.MobileInformer"
android:versionCode="2"
android:versionName="1.01"
>
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8"/>
<application android:label="#string/app_name" android:theme="#style/WatcomTheme" android:icon="#drawable/icon">
<supports-screens android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="false"/>
<activity android:name=".AuthorizationActivity"
android:label="#string/app_name" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<action android:name="com.watcom.MobileInformer.UPDATE_WIDGET"/>
</intent-filter>
</activity>
<activity android:launchMode="singleTop" android:name=".WidgetConfigureActivity" android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.appwidget.action.APPWIDGET_CONFIGURE" />
</intent-filter>
</activity>
<activity android:name=".ProjectsActivity" android:screenOrientation="portrait"/>
<receiver
android:icon="#drawable/icon"
android:label="Watcom informer"
android:name=".MyWidgetProvider" >
<intent-filter >
<action android:name="android.appwidget.action.APPWIDGET_UPDATE" />
<action android:name="ru.example.android.widget.ACTION_WIDGET_RECEIVER"/>
</intent-filter>
<intent-filter>
<action android:name="com.watcom.MobileInformer.UPDATE_WIDGET"/>
</intent-filter>
<meta-data
android:name="android.appwidget.provider"
android:resource="#xml/widget_provider" />
</receiver>
</application>
<uses-permission xmlns:android="http://schemas.android.com/apk/res/android"
android:name="android.permission.INTERNET" />
</manifest>
widget_provider.xml
<?xml version="1.0" encoding="utf-8"?>
<appwidget-provider xmlns:android="schemas.android.com/apk/res/android"
android:minHeight="320dp"
android:minWidth="100dp"
android:updatePeriodMillis="1800000"
android:initialLayout="#layout/widget_layout"
android:configuration="com.watcom.MobileInformer.WidgetConfigureActivity"/>
WidgetConfigureActivity
public class WidgetConfigureActivity extends Activity {
int widgetId;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setResult(RESULT_CANCELED);
setContentView(R.layout.login_layout);
Intent intent = getIntent();
Bundle extras = intent.getExtras();
if (extras!=null)
widgetId = extras.getInt(AppWidgetManager.EXTRA_APPWIDGET_ID,
AppWidgetManager.INVALID_APPWIDGET_ID);
if (widgetId == AppWidgetManager.INVALID_APPWIDGET_ID) {
finish();
}
Intent resultValue = new Intent();
resultValue.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId);
setResult(RESULT_OK, resultValue);
finish();
}
I've been struggling with this error for a lot and I just gave up. Every time I try to send a message using GCM this error appears on LogCat. What I'm failing to do? I've followed Android examples to set up GCM Notifications.
This is the LogCat Error
Edit: The message actually gets through but I don't think this error is normal.
08-12 17:13:15.888: W/GTalkService(2237): [DataMsgMgr] broadcast intent callback: result=CANCELLED forIntent { act=com.google.android.c2dm.intent.RECEIVE cat=[com.testing.encuesta] (has extras) }
AndroidManifest.xml
<permission android:name="com.testing.encuesta.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.testing.encuesta.permission.C2D_MESSAGE" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.VIBRATE"></uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.testing.encuesta.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=".Inicio">
</activity>
<receiver android:name=".GCMBroadcastReceiver" android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.testing.encuesta" />
</intent-filter>
<intent-filter>
<action android:name="com.google.android.c2dm.intent.REGISTRATION" />
<category android:name="com.testing.encuesta" />
</intent-filter>
</receiver>
My class GCMBroadcastReceiver
public class GCMBroadcastReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
try {
String action=intent.getAction();
if(action.equals("com.google.android.c2dm.intent.REGISTRATION"))
{
String registrationID=intent.getStringExtra("registration_id");
Log.d("ID",registrationID);
String error=intent.getStringExtra("error");
String unregistered=intent.getStringExtra("unregistered");
}
else if(action.equals("com.google.android.c2dm.intent.RECEIVE"))
{
String data1=intent.getStringExtra("data1");
String data2=intent.getStringExtra("data2");
Toast.makeText(context, data1, Toast.LENGTH_LONG);
}
} catch (Exception e) {
Log.d("Error", "error en C2DM"+e.toString());
}
}
Fixed, you just need to add setResultCode(Activity.RESULT_OK); at the end of the onReceive(); method