When phone state is ringing, I want to run an activity to show my own screen.
I'm using:
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
and
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
but notification bar does not hide when activity is shown.
Go through following code snippets:
Create a BroadCastReceiver Class to listen to incoming calls with
the highest priority:
Manifest.xml:
<receiver android:name=".MyPhoneBroadcastReceiver">
<intent-filter android:priority="99999">
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
Then in class, onReceive() method opens the CustomCallsReceiver Activity with intent action is "android.intent.action.ANSWER"
#Override
public void onReceive(final Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
final String incomingNumber = extras.getString("incoming_number");
Handler callActionHandler = new Handler();
Runnable runRingingActivity = new Runnable(){
#Override
public void run() {
//Notice the intent, cos u will add intent filter for your class(CustomCallsReceiver)
Intent intentPhoneCall = new Intent("android.intent.action.ANSWER");
intentPhoneCall.putExtra("INCOMING_NUM", incomingNumber);
intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentPhoneCall);
}
};
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
//increase the delay amount if problem occur something like -the screen didn't show up- that's the key about this method(the delay).
callActionHandler.postDelayed(runRingingActivity, 100);
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
callActionHandler.removeCallbacks(runRingingActivity);
}
}
}
Add this intent filter in to manifest file for the class you will
use as a Custom call receiver.
<activity android:name="CustomCallsReceiver" android:noHistory="true" android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.ANSWER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
The CustomeCallsReceiver Class:
public class CustomCallsReceiver extends Activity {
private String TAG = "CustomCallsReceiver";
String incomingNumber, caller;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custome_calls_receiver);
TextView number = (TextView) findViewById(R.id.number);
number.setGravity(Gravity.CENTER);
incomingNumber = getIntent().getExtras().getString("INCOMING_NUM");
caller = getCallerName(incomingNumber);
if (caller != null) {
number.setText(caller + "\n" + incomingNumber); }
}
And finally of course don't forget to add the Theme for not title or
notification bar at the manifest file
<application
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
Hope this working for you...
Related
I have two activities and service. I'm trying to make direct sharing, but it doesn't work(
First activity that sending data
public class MainShareActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share_main);
}
public void share(View view){
startService(new Intent(this, DirectSharePicker.class));
EditText etShare = (EditText) findViewById(R.id.sharedText);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, etShare.getText().toString());
startActivity(Intent.createChooser(intent, "Direct share demo"));
}
Activity, that catches SEND
public class CatchSendActivity extends AppCompatActivity {
public static final String EXTRA_USER_ID = "userId";
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shared);
TextView textView = (TextView) findViewById(R.id.result);
Intent intent = getIntent();
int userID = intent.getIntExtra(EXTRA_USER_ID, -1);
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
textView.setText(userID + ": " + sharedText);
}
Target service
public class DirectSharePicker extends ChooserTargetService {
#Override
public List<ChooserTarget> onGetChooserTargets(ComponentName componentName, IntentFilter intentFilter) {
List<ChooserTarget> response = new LinkedList<>();
Icon icon = Icon.createWithResource(this, R.id.icon);
for (int i = 0; i < 5; i++){
String title = "Target" + i;
Intent intent = new Intent(this, CatchSendActivity.class);
intent.putExtra(CatchSendActivity.EXTRA_USER_ID, i);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, intent, 0);
ChooserTarget chooserTarget = new ChooserTarget(title, icon, i, pendingIntent);
response.add(chooserTarget);
}
return response;
}
And this is my manifest
<activity
android:name=".MainShareActivity"
android:label="#string/app_name"/>
<service
android:name=".DirectSharePicker"
android:label="SHARING!"
android:permission="android.permission.BIND_CHOOSER_TARGET_SERVICE">
<intent-filter>
<action android:name="android.service.chooser.ChooserTargetService" />
</intent-filter>
</service>
<activity
android:name=".CatchSendActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain" />
</intent-filter>
<meta-data
android:name="android.service.chooser.chooser_target_service"
android:value=".DirectSharePicker" />
</activity>
But onGetChooserTargets never called.
From the documentation on Android Developer Pages:
The creator of a target may supply a ranking score. This score is assumed to be relative to the other targets supplied by the same query. Scores should be in the range from 0.0f (unlikely match) to 1.0f (very relevant match). Scores for a set of targets do not need to sum to 1.
Try putting some valid score instead 'i' since i can go upto 4 as per your code.
Also, put full name of classes and services in manifest
e.g
<service
android:name="com.your.package.DirectSharePicker"
...
Instead
<service
android:name=".DirectSharePicker"
...
i am developing an application, when the app is open there may be any missed call the notification is displayed,how i hide or remove the notification bar & is there any way to implement it.
i have put all codes like below in my application,
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
and
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen"
but i open an activity file using broadcast receiver, then the notification is seen when missed call or message arrives
You can use this code:
public class FullScreen
extends android.app.Activity
{
#Override
public void onCreate(android.os.Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
I was having the exact same problem as yours, but I hade it for both incoming and outgoing calls. I found the solution for the incoming calls/ missed calls but not yet for the outgoing calls.
What you are going to do is the following: 1.Create a BroadCastReceiver Class to listen to incoming calls with the highest priority:
a.In the Manifest.xml add:
<receiver android:name=".MyPhoneBroadcastReceiver">
<intent-filter android:priority="99999">
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
b.Then the Class
#Override
public void onReceive(final Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
final String incomingNumber = extras.getString("incoming_number");
Handler callActionHandler = new Handler();
Runnable runRingingActivity = new Runnable(){
#Override
public void run() {
//Notice the intent, cos u will add intent filter for your class(CustomCallsReceiver)
Intent intentPhoneCall = new Intent("android.intent.action.ANSWER");
intentPhoneCall.putExtra("INCOMING_NUM", incomingNumber);
intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentPhoneCall);
}
};
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
//increase the delay amount if problem occur something like -the screen didn't show up- that's the key about this method(the delay).
callActionHandler.postDelayed(runRingingActivity, 100);
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
callActionHandler.removeCallbacks(runRingingActivity);
}
}
}
2.a.In the Manifest.xml file add this intent filter for the class you will use as a custome call receiver.
<activity android:name="CustomCallsReceiver" android:noHistory="true" android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.ANSWER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
2.b.The CustomeCallsReceiver Class:
public class CustomCallsReceiver extends Activity {
private String TAG = "CustomCallsReceiver";
String incomingNumber, caller;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custome_calls_receiver);
TextView number = (TextView) findViewById(R.id.number);
number.setGravity(Gravity.CENTER);
incomingNumber = getIntent().getExtras().getString("INCOMING_NUM");
caller = getCallerName(incomingNumber);
if (caller != null) {
number.setText(caller + "\n" + incomingNumber); } }
3.And finally of course don't forget to add the Theme for not title or notification bar at the Manifest.XML file
<application
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
You need to set the theme in Android manifest .xml.. .
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen"
Hope this will help you..
if you set this as application theme it will give effect all the pages of your app..
<application
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
>
I am having difficulty getting a BroadcastReceiver to process my IntentService response. The service processes multiple different actions and returns an action type. The receiver will never seem to pick it up, however. The intent does get called as I can debug and set a breakpoint in the IntentService and see the action get processed successfully. I just never see the textbox updated with the appropriate data or see the BroadcastReceiver evein being called.
IntentService
protected void onHandleIntent(Intent intent) {
String action = intent.getAction();
// Data the service was called with.
Bundle incomingData = intent.getExtras();
String key = incomingData.getString(KEY_APPKEY);
String secret = incomingData.getString(KEY_SECRET);
String collection = incomingData.getString(KEY_COLLECTION);
CheckinManager cm = new CheckinManager(this.getApplicationContext(),key,secret,collection);
Intent broadcastIntent = new Intent();
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
if (action == ACTION_GET_POI) {
Double lat = incomingData.getDouble(KEY_LATITUDE);
Double lon = incomingData.getDouble(KEY_LONGITUDE);
ArrayList<POI> nearbyPOIs = new ArrayList<POI>();
//broadcastIntent.setAction(ACTION_GET_POI_PROCESSED);
broadcastIntent.setAction("com.msalinger.checkinmanager.CheckinService.getPOIProcessed");
try {
nearbyPOIs = cm.getPOI(lat, lon);
broadcastIntent.putExtra(OUT_KEY_RESULT, true);
broadcastIntent.putExtra(OUT_KEY_ERROR, "");
broadcastIntent.putParcelableArrayListExtra(OUT_KEY_POILIST, nearbyPOIs);
} catch (JSONException ex) {
Log.d(TAG,ex.getMessage() + "\n" + ex.getStackTrace());
broadcastIntent.putExtra(OUT_KEY_RESULT, false);
broadcastIntent.putExtra(OUT_KEY_ERROR, ex.getMessage());
}
}
else if (action == ACTION_CHECK_IN) {
// Do something
}
else if (action == ACTION_GET_CHECKINS) {
// Do Something
}
else if (action == ACTION_FIND_NEARBY_POIS_WITH_CHECKINS) {
// Do Something
}
sendBroadcast(broadcastIntent);
}
Broadcast Receiver as sub-class of Main Activity
public class CheckinReceiver extends BroadcastReceiver {
private final static String INTENT_BASE_URI = "com.msalinger.checkinmanager.CheckinService";
private final static String ACTION_GET_POI_PROCESSED = ".getPOIProcessed";
private final static String ACTION_CHECK_IN_PROCESSED = ".checkInProcessed";
private final static String ACTION_GET_CHECKINS_PROCESSED = ".getCheckinsProcessed";
private final static String ACTION_FIND_NEARBY_POIS_WITH_CHECKINS_PROCESSED = ".findNearbyPOIsWithCheckinsProcessed";
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("com.msalinger.checkinmanager.CheckinService.getPOIProcessed")) {
tv = (TextView)findViewById(R.id.textBox1);
Bundle incomingData = intent.getExtras();
String st = "";
if (incomingData.getBoolean("result")) {
ArrayList<POI> poiList = incomingData.getParcelableArrayList("poList");
st = printPOI(poiList);
}
else {
st = incomingData.getString("error");
}
}
else if (intent.getAction().equals(INTENT_BASE_URI + ACTION_CHECK_IN_PROCESSED)) {
}
else if (intent.getAction().equals(INTENT_BASE_URI + ACTION_GET_CHECKINS_PROCESSED)) {
}
else if (intent.getAction().equals(INTENT_BASE_URI + ACTION_FIND_NEARBY_POIS_WITH_CHECKINS_PROCESSED)) {
}
}
}
Manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.msalinger.checkinmanagerdemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service
android:enabled="true"
android:name="com.msalinger.checkinmanager.CheckinService" />
<receiver
android:name=".CheckinReceiver">
<intent-filter>
<action android:name="com.msalinger.checkinmanager.CheckinService.getPOIProcessed" />
</intent-filter>
<intent-filter>
<action android:name="com.msalinger.checkinmanager.CheckinService.checkInProcessed" />
</intent-filter>
<intent-filter>
<action android:name="com.msalinger.checkinmanager.CheckinService.getCheckinsProcessed" />
</intent-filter>
<intent-filter>
<action android:name="com.msalinger.checkinmanager.CheckinService.findNearbyPOIsWithCheckinsProcessed" />
</intent-filter>
</receiver>
</application>
</manifest>
What am I doing wrong? Note that the IntentService exists as part of an Android class library with a different package than the Main activity.
Because the receiver exists to update data in the activity, it should be registered when the activity resume and unregistered when the activity pause. And it should not be in the manifest (see the doc for this).
If it's not the case, it shouldn't be a subclass of your activity.
In all cases, I think your Receiver is not called because it has not the right name in the manifest. It may be something like this : .MainActivity$CheckinReceiver.
I have spent the last couple of hours looking through other questions on this topic and none that I found were able to give me any answers.
What is the best way to pass a string from an activity to a broadcast receiver in the background?
Here is my main activity
public class AppActivity extends DroidGap {
SmsReceiver mSmsReceiver = new SmsReceiver();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView scroll;
scroll = new ScrollView(this);
Bundle bundle = getIntent().getExtras();
final String ownAddress = bundle.getString("variable");
registerReceiver(mSmsReceiver, new IntentFilter("MyReceiver"));
Intent intent = new Intent("MyReceiver");
intent.putExtra("passAddress", ownAddress);
sendBroadcast(intent);
Log.v("Example", "ownAddress: " + ownAddress);
}
}
Here is my broadcast receiver
public class AppReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
final String ownAddress = intent.getStringExtra("passAddress");
Toast test = Toast.makeText(context,""+ownAddress,Toast.LENGTH_LONG);
test.show();
Log.v("Example", "ownAddress: " + ownAddress);
}
}
Here is the manifest for my receiver
<service android:name=".MyService" android:enabled="true"/>
<receiver android:name="AppReceiver">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_SENT"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
<receiver>
<service android:name=".MyServiceSentReceived" android:enabled="true"/>
<receiver android:name="AppReceiver">
<intent-filter android:priority="2147483645">
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
<action android:name="android.intent.action.BOOT_COMPLETED" />
</intent-filter>
</receiver>
When the broadcast receiver logs an event the app crashes. I need to have it run behind the scenes and pull a string from my main activity.
Can anyone help me with this or point me in the right direction?
Addition from comments and chat
Your String ownAddress will always be null unless the Intent has an String with the key passAddress in the the Bundle extras. Anytime your Receiver catches an Intent (whether from SMS_SENT, SMS_RECEIVED, or BOOT_COMPLETED) ownAddress will be null because the OS doesn't provide a String extra named passAddress. Hope that clears things up.
Original Answer
I haven't used DroidGap but this is what you want for a regular Android Activity.
Activity:
public class AppActivity extends Activity {
AppReceiver mAppReceiver = new AppReceiver();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
registerReceiver(mAppReceiver, new IntentFilter("MyReceiver"));
String string = "Pass me.";
Intent intent = new Intent("MyReceiver");
intent.putExtra("string", string);
sendBroadcast(intent);
}
}
Receiver:
public class AppReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, intent.getStringExtra("string"), Toast.LENGTH_LONG).show();
}
}
Don't forget to unregister the receiver in onDestroy(), like this:
#Override
protected void onDestroy() {
unregisterReceiver(mAppReceiver);
super.onDestroy();
}
Here is the scenario :
An activity is displayed(active). If a phone call comes, the activity should receive the intent (send the "incoming call screen" to the background/hide it from the display) and itself remain visible to the user. I dont necessarily want to suppress the incoming phone call as I have read in a lot of questions that it is not possible with public APIs.
All I want is to somehow make that android's default incoming call screen hidden by my activity on the top.
This behavior is only required when my activity is visible which is NOT EQUAL TO having a PHONE_STATE broadcast receiver to start my activity. The latter question has been answered a number of times on SO.
Please help me. I have been looking for directions for almost a day now.
Thanks for your time.
This is how I solved it :
Manifest.xml
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
...
<receiver android:name=".MyPhoneBroadcastReceiver">
<intent-filter android:priority="99999">
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
<activity android:name=".LockScreenActivity" android:noHistory="true" android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.ANSWER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
MyPhoneBroadcastReceiver.java
public void onReceive(final Context context, Intent intent) {
Bundle extras = intent.getExtras();
...
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
new Handler().postDelayed(new Runnable() {
public void run() {
Intent intentPhoneCall = new Intent("android.intent.action.ANSWER");
intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentPhoneCall);
}
}, 100);
}
}
}
LockScreenActivity.java - A regular activity class with UI that shows up saying your screen is locked. It covers 100% area of your screen i.e. no navigation/status bar. Also the HOME/MENU keys have been disabled. This is how I achieved that : How can I detect user pressing HOME key in my activity?
P.S. : The trick is not the main logic but a 100ms delay. Without it your custom(home) lock screen will be removed by the system default incoming call screen everytime you get a call on the phone!
Ya sanjana is right that code works for me.. but instead of giving postdelay 100msec give more then 1.5 sec.. Bcoz to start incoming screen needs 800 to 1000msec.. I copied her code only but modified a bit.. Try this works fine...
#Override
public void onReceive(final Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
final String incomingNumber = extras.getString("incoming_number");
Handler callActionHandler = new Handler();
Runnable runRingingActivity = new Runnable() {
#Override
public void run() {
Intent intentPhoneCall = new Intent("android.intent.action.ANSWER");
intentPhoneCall.putExtra("INCOMING_NUM", incomingNumber);
intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intentPhoneCall);
}
};
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
callActionHandler.postDelayed(runRingingActivity, 2000);
}
if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
callActionHandler.removeCallbacks(runRingingActivity);
// setResultCode(Activity.RESULT_CANCELED);
}
}
}
Then use PhoneStateListener at receiver class side.. I used like this....
/*********** My receiver class onCreate() contains ************/
TelephonyManager telephony = (TelephonyManager)
getSystemService(getApplicationContext().TELEPHONY_SERVICE);
telephony.listen(myListener, PhoneStateListener.LISTEN_CALL_STATE);
class MyPhoneStateListener extends PhoneStateListener {
Activity curActivity;
public MyPhoneStateListener(Activity _curActivity){
this.curActivity = _curActivity;
}
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.i("TEST APP", "Cal End");
curActivity.finish();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i("TEST APP", "Hello");
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.i("TEST APP", "Calling...");
break;
}
}
}