Receiving Android SMS - java

I have been trying so many different things to get this demo running but i couldn't manage to make it run properly..
This is my Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.messagekeeper"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="9" />
<uses-permission android:name="android.permission.RECEIVE_SMS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.messagekeeper.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>
<receiver android:name=".SmsReciever">
<intent-filter>
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
</application>
</manifest>
This is my MainActivity class (Done nothing with it):
package com.example.messagekeeper;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
And This is the SmsReciever class:
package com.example.messagekeeper;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;
class SmsReciever extends BroadcastReceiver{
#Override
public void onReceive(Context context, Intent intent){
Object[] pdus=(Object[])intent.getExtras().get("pdus");
SmsMessage shortMessage=SmsMessage.createFromPdu((byte[]) pdus[0]);
Log.d("SMSReceiver","SMS message sender: "+
shortMessage.getOriginatingAddress());
Log.d("SMSReceiver","SMS message text: "+
shortMessage.getDisplayMessageBody());
}
}
When i get a message, i want it to see it on my app. In this situation, i mean when i use this codes and run my app on emulator, and send a message from one emulator to another, my app crashes and i get this error: http://puu.sh/1yZub

If you've copied your code exactly as it stands, you might be just missing the public keyword at the beginning.
Change class SmsReciever extends BroadcastReceiver{
to public class SmsReciever extends BroadcastReceiver{

Related

onServiceConnected not called after enable my AccessibilityService

After I open my app, it jumps to the accessibility settings. But the onServiceConnected is not called after I have turned on my AccessibilityService (which is called VoiceService).
Could you please tell me what should I do? It seems that the VoiceService fails to start although I enable it in XML. Or I need to bind the MainActivity with the VoiceService in a different way?
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wang.hearexpr">
<uses-permission
android:enabled="true"
android:name="android.permission.BIND_ACCESSIBILITY_SERVICE"/>
<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" />
</intent-filter>
</activity>
<service android:name=".VoiceService"
android:enabled="true"
android:permission="android.permission.BIND_ACCESSIBILITY_SERVICE"
android:label="#string/service_name">
<meta-data
android:name="android.accessibilityservice"
android:resource="#xml/voice_config"/>
<intent-filter>
<action android:name="android.accessibilityservice.AccessibilityService"/>
</intent-filter>
</service>
</application>
</manifest>
voice_config.xml
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:description="#string/service_description"
android:packageNames="com.example.wang.hearexpr"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFlags="flagDefault"
android:accessibilityFeedbackType="feedbackSpoken"
android:notificationTimeout="100"
android:canRetrieveWindowContent="true"
android:canRequestTouchExplorationMode="true"
android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity"/>
VoiceService.java
package com.example.wang.hearexpr;
import android.accessibilityservice.AccessibilityService;
import android.accessibilityservice.AccessibilityServiceInfo;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;
import android.view.accessibility.AccessibilityEvent;
public class VoiceService extends AccessibilityService {
private static final String TAG = "hear the voice";
#Override
public void onServiceConnected(){
super.onServiceConnected();
Log.d(TAG, "Service Connected");
}
#Override
public void onAccessibilityEvent(AccessibilityEvent event) {
Log.d(TAG,"onAccessibilityEvent: "+ event.toString());
}
#Override
public void onInterrupt() {
Log.d(TAG, "Interrupted");
}
#Override
public void onDestroy(){
Log.d(TAG, "Destroyed");
}
}
MainActivity.java
package com.example.wang.hearexpr;
import android.content.Intent;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
private static final Intent sSettingsIntent =
new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startActivityForResult(sSettingsIntent,0);
}
}
This is the answer:
in the voice_config.xml
android:settingsActivity="com.example.android.accessibility.ServiceSettingsActivity" should be modified as your own activity name.

Android Block Incoming SMS using BroadCastReceiver

I want to block incoming SMS message as long as my application is running . I could achieve that but the problem is after I close the app or even restart or even uninstall it the user will not be able to receive SMS message anymore . So How can I make the application to block incoming SMS only when it's running and when it gets closed or uninstalled etc.. to stop blocking SMS. Here's my code :
BroadCastReceiver.Java
package com.example.sms;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
public class BroadCastReceiver extends BroadcastReceiver
{
public void onReceive(Context context, Intent intent)
{
abortBroadcast();
}
}
MainActivity.java
package com.example.sms;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
Manifiest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sms"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<receiver android:name=".BroadCastReceiver">
<intent-filter android:priority="2147483647">
<action android:name="android.provider.Telephony.SMS_RECEIVED" />
</intent-filter>
</receiver>
<activity
android:name="com.example.sms.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>
Note: I've tested the application on android 2.2 on my android emulator "Sent SMS messages using Telnet"
Check if your app is running then only abort broadcast receiver else not
//define this variable above onReceive() with default value as false;
boolean appRunningInBack=false;
ActivityManager am = (ActivityManager) mContext.getSystemService(Activity.ACTIVITY_SERVICE);
String packageName = am.getRunningTasks(1).get(0).topActivity.getPackageName();
if(packageName.equalIgnoreCase("your app package name") || appRunningInBack)
{
appRunningInBack=true;
abortBroadCast();
} else {
}
Also add Permission in AndroidManifest.xml
<uses-permission android:name="android.permission.GET_TASKS" />
After this when your app goes in background.or while running .this variable will be true.

Android Phone Call Intent

I am trying to make a call with the help of button, compiling time shows no errors but the application stops working as soon as i start the app, both on emulator and my android device. i have also set the permissions in the manifest file. but still :\
please help
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/makeCall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/make_call" />
</LinearLayout>
MainActivity.java
package com.example.phonecalldemo;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public abstract class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startBtn = (Button) findViewById(R.id.makeCall);
startBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
makeCall();
}
});
}
protected void makeCall() {
Log.i("Make call", "");
Intent phoneIntent = new Intent(Intent.ACTION_CALL);
phoneIntent.setData(Uri.parse("tel:8866158630"));
startActivity(phoneIntent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.phonecalldemo"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<permission android:name="android.permission.CALL_PHONE"></permission>
<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.phonecalldemo.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>
You can't instantiate abstract classes. Remove the 'abstract' from "public abstract class MainActivity", which will give you a different error telling you to implement the onClick method. Once you add that method that your app will launch.

Send Array to Different Application

I would like to send an array in different application.
So In the first application I have MainActivity. The Activity will start an intent. When I'm trying to make an intent, I found a problem. The problem is ShowArrayActivity "cannot be resolved to a type". Actually ShowArrayActivity class is in another apppication. I have tried to add project in build path, but I have an error message said that a cycle was detected in the build path of project 'sendIntent'..
package com.example.sendintent;
import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import com.example.sendintent.DisplayMessageActivity;
public class MainActivity extends Activity {
public static final String ARRAYS_COUNT = "com.example.sendintent.MainActivity.ARRAYS_COUNT";
public static final String ARRAY_INDEX = "com.example.sendintent.MainActivity.ARRAY_INDEX";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void sendMessage(View view)
{
final String data[][] = new String[][]
{{"Car","75","loaded"},
{"Motor","25","loaded"},
{"Truck","30","loaded"},
{"Bike","40","loaded"},
{"Boat","10","loaded"}};
Bundle bundle = new Bundle();
int count = data.length;
bundle.putInt(ARRAYS_COUNT, count);
for (int i = 0; i < count; i++)
bundle.putStringArray(ARRAY_INDEX + i, data[i]);
Intent intent = new Intent(this, ShowArrayActivity.class); ---->ERROR
intent.putExtras(bundle);
startActivity(intent);
}
}
In Manifest I have declare
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sendintent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.sendintent.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="com.example.sendintent.DisplayMessageActivity"
android:parentActivityName="com.example.sendintent.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.sendintent.MainActivity" />
</activity>
<service android:name="com.example.sendintent.MainActivity" />
<receiver android:name="com.example.receiver.MainActivityReceiver">
<intent-filter>
<action android:name="com.example.sendintent.MainActivity.ARRAYS_COUNT"/>
<action android:name="com.example.sendintent.MainActivity.ARRAYS_INDEX"/>
</intent-filter>
</receiver>
</application>
</manifest>
In another application, I have declare the receiver for the intent. It called MainActivityReceiver
package com.example.receiver;
import java.util.ArrayList;
import com.example.sendintent.MainActivity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
public class MainActivityReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
intent.setClass(context, MainActivity.class);
context.startService(intent);
}
}
I have another class called ShowArrayAcivity:
package com.example.receiver;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class ShowArrayActivity extends Activity {
public String[][] data;
public static final String ARRAYS_COUNT = "com.example.sendintent.MainActivity.ARRAYS_COUNT";
public static final String ARRAY_INDEX = "com.example.sendintent.MainActivity.ARRAY_INDEX";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
int count = bundle.getInt(ShowArrayActivity.ARRAYS_COUNT, 0);
ArrayList<String[]> arrays = new ArrayList<String[]>(count);
for (int i = 0; i < count; i++)
arrays.add(bundle.getStringArray(ShowArrayActivity.ARRAY_INDEX + i));
data = arrays.toArray(new String[][]{});
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
In Manifest for receiver application
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.receiver"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.receiver.ShowArrayActivity"
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="com.example.sendintent.MainActivity" />
<receiver android:name="com.example.receiver.MainActivityReceiver">
<intent-filter>
<action android:name="com.example.sendintent.MainActivity.ARRAYS_COUNT"/>
<action android:name="com.example.sendintent.MainActivity.ARRAYS_INDEX"/>
</intent-filter>
</receiver>
</application>
</manifest>
In receiver application, It won't show the array from sendIntent application in Interface. I just want to know the way to use broadcast and intent. So if I run in debug mode,in ShowArrayActivity, the variable data will containt the result of the intent.
So my questions is,why I can't use another class when I try to send the intent? Did I forgot something to implement for madethe communication between 2 application?
Intent intent = new Intent(this, ShowArrayActivity.class);
this way you tell activity to find ShowArrayActivity within this Application and launch it. It fails as Application can't find it in it's manifest.
Create Intent with action.
Intent intent = new Intent (NAME_OF_THE_FILTER);
then replace string it second manifest
assuming
public static final String NAME_OF_THE_FILTER = "com.myapp.myfilter"
<intent-filter>
<action android:name="com.myapp.myfilter"/>
</intent-filter>

Start new Activity from menu

I wrote simple android app with bugs. When open second activity from menu- android stopped apps.
Code:
Manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Activit"
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>
<activity android:name=".Activit2"/>
</manifest>
First Activity:
package com.example;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView.AdapterContextMenuInfo;
public class Activit extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.me, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
switch(item.getItemId()){
case R.id.ok: Activ();
};
return super.onOptionsItemSelected(item);
}
private void Activ(){
Intent inten=new Intent();
inten.setClass(Activit.this, Activit2.class);
startActivity(inten);
}
}
Second Activity with temporary layout(it the same in the first):
package com.example;
import android.app.Activity;
import android.os.Bundle;
public class Activit2 extends Activity{
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
your tag <Activity /> should be in ( not out ) of the tag <application> :
try this :
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".Activit"
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=".Activit2"/> // you should declare your activity Here
</application>
The tag <activity android:name=".Activit2"/> should be inside the <application> tag in your Manifest.xml
Your second activity definition is outside the application definition in your AndroidManifest.xml.

Categories

Resources