In my application i using a broadcastreceiver to receive a incoming call and shown a some details to user. It works fine, but when i opened the application and incoming call is received, it shows at top of application, not the top of incoming call screen. How to solve this issue ?
thanks in advance
Broadcast Receiver
public void onReceive(final Context context, final Intent intent) { // 1
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); // 2
if (TelephonyManager.EXTRA_STATE_RINGING.equals(state)) { // 3
String incomingNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER); // 4
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(context, Test1.class);
i.putExtras(intent);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(i);
}
}, 2000);
}
}
Message Showing Activity
public class Test1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.popup);
WindowManager.LayoutParams wmlp = getWindow().getAttributes();
wmlp.gravity = Gravity.BOTTOM | Gravity.LEFT;
String number = getIntent().getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
TextView tv1 = (TextView)findViewById(R.id.tv1);
tv1.setText(number);
TextView tv2 = (TextView)findViewById(R.id.tv2);
tv2.setText(MyApp.mDbh.check(number));
new Handler().postDelayed(new Runnable(){
public void run() {
finish();
}
}, 10 *1000);
}
}
Related
I am developing an Android Wear OS 2.0 application. Every time a user gets an SMS from a given number, the watch should start vibrating, and a UI with a given text should appear, with a button, which stops the vibration. It works in the following way:
In the SmsReciever.java I'm checking if the phone number is matching, or the UI screen is already active.
public class SmsReceiver extends BroadcastReceiver {
//interface
private static SmsListener mListener;
#Override
public void onReceive(Context context, Intent intent) {
Bundle data = intent.getExtras();
Object[] pdus = (Object[]) data.get("pdus");
for(int i=0;i<pdus.length;i++){
SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i], "3gpp");
String sender = smsMessage.getDisplayOriginatingAddress();
String alertPhoneNumber = "301112233";
if (sender.toLowerCase().contains(alertPhoneNumber.toLowerCase()))
{
String messageBody = smsMessage.getMessageBody();
//Pass the message text to interface
mListener.messageReceived(messageBody);
} else if (AlarmActivity.active) {
Intent intent1 = new Intent();
intent1.setClassName("hu.asd.watchtest", "hu.asd.watchtest.AlarmActivity");
intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent1);
}
}
}
public static void bindListener(SmsListener listener) {
mListener = listener;
}
}
I have needed the else if part, because sometimes when the UI was active, and I've received a new message, the vibration stopped. So that part starts the AlarmActivity (which handles the vibrating).
In the MainActivity.java I'm binding a new listener, so now every time I get the right message in the SmsReciever, the AlarmActivity should run:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermissions(Manifest.permission.RECEIVE_SMS, PERMISSIONS_REQUEST_RECEIVE_SMS);
SmsReceiver.bindListener(new SmsListener() {
#Override
public void messageReceived(String messageText) {
Intent myIntent = new Intent(MainActivity.this, AlarmActivity.class);
MainActivity.this.startActivity(myIntent);
}
});
}
In the AlarmActivity.java the application wakes the screen up, then gets the Vibrator, sets the onClickListeners to the stop vibrating button, and then starts the actual vibration. I also change the active state here:
public class AlarmActivity extends WearableActivity {
static boolean active = false;
#Override
public void onResume() {
super.onResume();
active = true;
}
#Override
public void onStart() {
super.onStart();
active = true;
}
#Override
public void onStop() {
super.onStop();
active = false;
}
#Override
public void onPause() {
super.onPause();
active = false;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_alarm);
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
final PowerManager.WakeLock wakeLock = powerManager.newWakeLock(PowerManager.ACQUIRE_CAUSES_WAKEUP|PowerManager.SCREEN_BRIGHT_WAKE_LOCK,
"MyWakelockTag");
wakeLock.acquire();
findViewById(R.id.stop_button).getBackground().setLevel(5000);
final Vibrator vibrator = (Vibrator) getSystemService(VIBRATOR_SERVICE);
final long[] mVibratePattern = new long[]{0, 400, 800, 600, 800, 800, 800, 1000};
final int[] mAmplitudes = new int[]{0, 255, 0, 255, 0, 255, 0, 255};
final Button stopButton = findViewById(R.id.stop_button);
stopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
vibrator.cancel();
wakeLock.release();
new SendEmail().execute();
Intent myIntent = new Intent(AlarmActivity.this, MainActivity.class);
AlarmActivity.this.startActivity(myIntent);
}
});
setAmbientEnabled();
vibrator.vibrate(VibrationEffect.createWaveform(mVibratePattern, mAmplitudes, 0)); // 0 to repeat endlessly.
}
}
If the user presses the Button, then the MainActivity starts again. The problem is, that out of 50 cases, I can send multiple text messages, the watch will vibrate until I press the stop button, but in 1 case, it'll only vibrate once, then it will stop vibrating. It usually happens, if the AlarmActivity is active already and vibrating, or vibration was cancelled before. I'm guessing that I do way to much work with intents, or something with the Vibrator instances? Or when I get a new text, the watch requires the Vibrator, and my Application can't get it?
This is my first Android application, and I tried a lot of different implementations, but still not perfect.
Currentlly I am implementing pattern lock application and I want to set limit when user enter wrong pattern many time.Example,If user enter wrong pattern at that time set limit(3 or 4 time limit) and set delay 30 second and after 30 second give permission to enter pattern.
So,If anyone know how i can do this please give idea of that.
Here this my Reciever
public class LockScreenReceiver extends DeviceAdminReceiver {
Context context;
#Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
Log.i("Action...","..."+action);
//If the screen was just turned on or it just booted up, start your Lock Activity
if(action.equals(Intent.ACTION_SCREEN_OFF) || action.equals(Intent.ACTION_BOOT_COMPLETED))
{
Intent i = new Intent(context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(i);
}
}
#Override
public void onPasswordFailed(Context ctxt, Intent intent) {
DevicePolicyManager mgr = (DevicePolicyManager) ctxt.getSystemService(Context.DEVICE_POLICY_SERVICE);
int no = mgr.getCurrentFailedPasswordAttempts();
if (no >= 3) {
context.startActivity(new Intent(context,ChangeActivity.class));
}
}
}
Service
public class LockScreenService extends Service {
DeviceAdminReceiver receiver;
#Override
public IBinder onBind(Intent intent) {
return null;
}
// Register for Lockscreen event intents
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
receiver = new LockScreenReceiver();
registerReceiver(receiver, filter);
startForeground();
return START_STICKY;
}
// Run service in foreground so it is less likely to be killed by system
private void startForeground() {
Notification notification = new NotificationCompat.Builder(this)
.setContentTitle(getResources().getString(R.string.app_name))
.setTicker(getResources().getString(R.string.app_name))
.setContentText("Running")
.setContentIntent(null)
.setOngoing(true)
.build();
startForeground(9999,notification);
}
#Override
#SuppressWarnings("deprecation")
public void onCreate() {
KeyguardManager.KeyguardLock key;
KeyguardManager km = (KeyguardManager)getSystemService(KEYGUARD_SERVICE);
//This is deprecated, but it is a simple way to disable the lockscreen in code
key = km.newKeyguardLock("IN");
key.disableKeyguard();
//Start listening for the Screen On, Screen Off, and Boot completed actions
IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(Intent.ACTION_BOOT_COMPLETED);
//Set up a receiver to listen for the Intents in this Service
receiver = new LockScreenReceiver();
registerReceiver(receiver, filter);
super.onCreate();
}
#Override
public void onDestroy() {
unregisterReceiver(receiver);
super.onDestroy();
}
}
Activity
public class MainActivity extends ActionBarActivity {
private Lock9View lock9View;
private static String MY_PREFS_NAME = "PatternLock";
private static String PATTERN_KEY;
SharedPreferences prefs;
Button btnChange;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(MainActivity.this, LockScreenService.class));
makeFullScreen();
setContentView(R.layout.activity_main);
btnChange = (Button)findViewById(R.id.btnChange);
btnChange.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent in = new Intent(MainActivity.this,ChangeActivity.class);
startActivity(in);
}
});
prefs = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
lock9View = (Lock9View) findViewById(R.id.lock_9_view);
lock9View.setCallBack(new Lock9View.CallBack() {
#Override
public void onFinish(String password) {
PATTERN_KEY = prefs.getString("Pattern", "invalid");
if (PATTERN_KEY.equals("invalid")) {
Toast.makeText(MainActivity.this, "Options --> Create new Pattern", Toast.LENGTH_LONG).show();
} else {
if (password.equals(PATTERN_KEY)) {
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
}else{
Context context = getApplicationContext();
// Create layout inflator object to inflate toast.xml file
LayoutInflater inflater = getLayoutInflater();
// Call toast.xml file for toast layout
View toastRoot = inflater.inflate(R.layout.layout_toast3, null);
Toast toast = new Toast(context);
// Set layout to toast
toast.setView(toastRoot);
toast.setGravity(Gravity.HORIZONTAL_GRAVITY_MASK | Gravity.BOTTOM,
0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.show();
}
}
}
});
}
private void makeFullScreen() {
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
if(Build.VERSION.SDK_INT < 19) { //View.SYSTEM_UI_FLAG_IMMERSIVE is only on API 19+
this.getWindow().getDecorView()
.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
} else {
this.getWindow().getDecorView()
.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE);
}
}
#Override
public void onBackPressed() {
return; //Do nothing!
}
public void unlockScreen(View view) {
//Instead of using finish(), this totally destroys the process
android.os.Process.killProcess(android.os.Process.myPid());
}
}
So,How i can achieve this...
Have a int field like failedCounter and increment it each time user inputs invalid patterns check if reached the limit then disable the input interface and have a handler to reset the value after the time delay.
int failedCount = 0;
final static int LIMIT = 5; //set your limit here
private void invalidPattern() {
if (++failedCount == LIMIT) {
//disable the input
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//reset the failed count
faildCount = 0;
//Enable the input interface here
}
}, 30000); // 30Sec delay
}
}
Use this two methods -
ScheduledThreadPoolExecutor c1;
private void IncorrectCallCounter() {
if (failedCounter>=0)
{
c1.shutdownNow();
LockScreenFor30Second();
}else
{
if (c1!=null)
c1.shutdownNow();
}
c1 = new ScheduledThreadPoolExecutor(1);
c1.schedule(new Runnable() {
#Override
public void run() {
failedCounter=0;
c1.shutdownNow();
}
}, 15, TimeUnit.SECONDS);
}
ScheduledThreadPoolExecutor c2;
private void LockScreenFor30Second() {
//Lock Screen Here
c2 = new ScheduledThreadPoolExecutor(1);
c2.schedule(new Runnable() {
#Override
public void run() {
//Unlock Screen Here
c2.shutdownNow();
}
}, 30, TimeUnit.SECONDS);
}
Declare failedCounter globally
private int failedCounter=0;
And call this method when you detect wrong pattern -
failedCounter=failedCounter+1;
IncorrectCallCounter();
If user enter wrong pattern 4 times in 15 seconds then this will call LockScreenFor30Second method. and inside LockScreenFor30Second add your code.
I want to replace the outgoing screen and make call through that custom screen. I succeed to bring custom screen but I could not make calls. If I use ACTION.CALL then it call the Default outgoing screen.
public class OutgoingCallBroadcastReciver extends BroadcastReceiver {
Context c;
public OutgoingCallBroadcastReciver() {
}
#Override
public void onReceive(final Context context, final Intent intent) {
final String number = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
c=context;
setResultData(null);
setResultData(number);
callActionHandler.postDelayed(runRingingActivity, 1000);
}
Handler callActionHandler = new Handler();
Runnable runRingingActivity = new Runnable(){
#Override
public void run()
{
Intent intentPhoneCall = new Intent(c, OutgoingScreen.class);
intentPhoneCall.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
c.startActivity(intentPhoneCall);
}
};
}
For outgoing calls I make custom work around and created custom permission of outgoing reciever in manifest.
I had called the activity after a delay using handler.
Hope i will work for you.
Please check the below code.
#Override
public void onReceive(Context context, Intent intent)
{
mcontext = context;
setResultData(null);
dialphonenumber = getResultData();
if dialphonenumber == null)
{
dialphonenumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
}
setResultData(dialphonenumber);
callActionHandler.postDelayed(runRingingActivity, 1000);
}
Handler callActionHandler = new Handler();
Runnable runRingingActivity = new Runnable()
{
#Override
public void run()
{
Intent intentPhoneCall = new Intent(mcontext, OutgoingCallActivity.class);
intentPhoneCall.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intentPhoneCall.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mcontext.startActivity(intentPhoneCall);
}
};
Hope it will work for you.
Let me know if you have any issue.
I have 2 activities.
In my first, i have a button and when i click on it, it start the second activity .
But when i go back to my first and i click for second time my button, my second activity start but i have to go back two time to go back in my firts activity.
If a click one again a will have to go back 3 time ...
help me please. and thank you in advance :)
this is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this._that = this;
_progressDialog = new ProgressDialog(this);
EditText editText = (EditText) findViewById(R.id.MainActivityEditText);
editText.setText("T_F81D4FA3F8");
Button button = (Button) findViewById(R.id.MainActivityButton);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.MainActivityButton ) {
Log.w("MainActivity", "onClick");
_progressDialog.setMessage("Chargement en cours");
_progressDialog.show();
new Thread(new Runnable() {
public void run() {
EditText editText = (EditText) findViewById(R.id.MainActivityEditText);
String s = editText.getText().toString().replace(" ", "");
Log.i("EditText", s);
ID_APPLICATION = s;
//if (! Datas.getInstance().isUpdateDatas())
WebService.getInstance().datas(_that);
LocalBroadcastManager.getInstance(_that).registerReceiver(datasUpdateFail, new IntentFilter("datas-update-fail"));
LocalBroadcastManager.getInstance(_that).registerReceiver(datasUpdate, new IntentFilter("datas-update"));
}
}).start();
}
}
BroadcastReceiver datasUpdate = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
myStartActivity("ACCUEIL");
_progressDialog.dismiss();
}
};
BroadcastReceiver datasUpdateFail = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(MainActivity.this, "Erreur : Veuillez verifier votre identifiant ou votre connexion", Toast.LENGTH_SHORT).show();
_progressDialog.dismiss();
}
};
public void myStartActivity(String page){
Intent intent = new Intent(this, PageActivity.class);
Bundle bundle = new Bundle();
bundle.putString("page", page);
intent.putExtras(bundle);
this.startActivity(intent);
}
unregister the receiver
#Override
protected void onPause() {
// Unregister receiver
LocalBroadcastManager.getInstance(this).unregisterReceiver(datasUpdate);
LocalBroadcastManager.getInstance(this).unregisterReceiver(datasUpdateFail);
super.onPause();
}
Every time you get into this activity you register a received. So the second time you come back, you have 2 receivers registered. Therefore when you click on button 2 activities will be opened.
Try uninteresting the regiseter when you are done with it. eg. Before starting the new activity.
LocalBroadcastManager.getInstance(_that).unregisterReceiver(datasUpdateFail);
LocalBroadcastManager.getInstance(_that).unregisterReceiver(datasUpdate);
I'm trying to start an activity from a service running in background but its not working.. here's the code. the service class uses an intent to call activity class. can anyone help me out??
//The service class
public class ServiceTemplate extends Service
{
// code to execute when the service is starting up
#Override
public void onStart(Intent intent, int startid)
{
Toast.makeText(getBaseContext(), "Accelerometer initiated", Toast.LENGTH_SHORT).show();
// to set a delay
Runnable mMyRunnable = new Runnable()
{
#Override
public void run()
{
Toast.makeText(getBaseContext(), "Accelerometer running", Toast.LENGTH_SHORT).show();
// Change state here
}
};
Handler myHandler = new Handler();
myHandler.postDelayed(mMyRunnable, 5000);
Intent i = new Intent(this, TimerAct.class);
startActivity(i);
}
}
//The Activity class
public class TimerAct extends Activity
{
static TextView timeDisplay;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.time);
int length = 30000;
timeDisplay = (TextView) findViewById(R.id.timer);
timeDisplay.setText("Time left: " + length / 1000);
}
}
Since a Service is not a UI component, you need to start a new task. Use FLAG_ACTIVITY_NEW_TASK:
Intent i = new Intent(this, TimerAct.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);