Activity won't start a service - android

I m trying to start an IntentService from the main activity of y application and it won't start. I have the service in the manifest file. Here's the code:
MainActivity
public class Home extends Activity {
private LinearLayout kontejner;
IntentFilter intentFilter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
kontejner = (LinearLayout) findViewById(R.id.kontejner);
intentFilter = new IntentFilter();
startService(new Intent(getBaseContext(), HomeService.class));
}
}
Service:
public class HomeService extends IntentService {
public HomeService() {
super("HomeService");
// TODO Auto-generated constructor stub
}
#Override
protected void onHandleIntent(Intent intent) {
Toast.makeText(getBaseContext(), "TEST", Toast.LENGTH_LONG).show();
}
}
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.salefinder"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Home"
android:label="#string/title_activity_home" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".HomeService" />
</application>
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
How can I make it work?

onHandleIntent gets called from a background thread. You can't modify the UI, or in this case, make Toast from outside the UI thread. So, I wouldn't expect anything to happen with your service.
Just try writing something out with Log.d() to see if your service is getting called.

It seams that android cached a bad version of the app - I forced closed it and started it again, and it worked...

Related

Sending notification from one app to another app in Android

I am creating android application for sending notification. I have create two Android application. Here I want to send some forms details via notification from first Android application to 2nd android application in Android.
And after that receiving the notification, click on the notification I want to open my second Android application. What should be done to send the push notification to the particular individual user in Android?
Here is my Activity one codes
public class Activity_One extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_one);
Button btn =(Button)findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.customer.Activity_One","com.vendor.Activity_B"));
startActivity(intent);
}
});
}
}
Here is AndroidManifest.xml file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.customer"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Activity_One"
android:label="#string/app_name"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Here is Activity two code
public class Activity_B extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_two);
Button bn = (Button)findViewById(R.id.button1Send);
bn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.setAction("com.customer.Activity_One");
startActivity(intent);
}
});
}
}
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vendor"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Activity_B"
android:label="#string/app_name"
android:launchMode="singleTask">
<intent-filter>
<action
android:name="com.vendor.Activity_B" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
You do not need to use notifications at all. You can just use intents. That is the standard way to accomplish what you want to achieve. See Interacting with Other Apps for details.
From first application you can start a normal notification and from second application initialize a broadcast receiver and read notification

Receive Broadcast while activity is not active

I wanted to intercept an incoming message so I created a class extending BroadcastReceiver(as given here) it worked but as I wanted to control the receiver(stop it when required) I implemented this but now when I exit the activity the receiver doesn't work.
How to implement the receiver such that it can be controlled and receive broadcast when the activity is not running?
Mainactivity
public class MainActivity extends Activity {
Button StopB;
IntentFilter STARTER;
final BroadcastReceiver MSgR=new BR();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
StopB=(Button) findViewById(R.id.button1);
final BroadcastReceiver MSgR=new BR();
STARTER=new IntentFilter();
STARTER.addAction("android.provider.Telephony.SMS_RECEIVED");
registerReceiver(MSgR, STARTER);
StopB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.i("Pressed", "UNRGSTR");
unregisterReceiver(MSgR);
}
} ) ;
}
BR.java
public class BR extends BroadcastReceiver{
String TAG="DELETE BLOCK";
#Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
// do something here
}
Manifest
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<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.delete_sms_2.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="BR"></receiver>
</application>
</manifest>
define when the receiver will start, you define when activity start then recive work, you need to change in menifest file , as like in this intent filter when sms recived, as like you change it accourding to ur need thanks
<receiver android:name=".HellowordActivity" >
<intent-filter >
<action android:name="android.provider.Telephony.SMS_RECEIVED"/>
</intent-filter>
</receiver>

When detect in/outgoing call in android phone i want to send a sms to my parent number

i am trying to develop a android application using broadcast receiver,my application means that,first i want to define and store a PARENT number to the database on my first activity ,when we install the app this activity will launch and the parent number saved to the database,then broadcast receiver want to active and its listen for call state changing,if any call is make through that phone i want to send a message to the parent number...??...here how can i use broadcast receiver...is it possible...and i mentioning my codes below......i hope you can help me for this .i try to do this many ways but i cant get the result.here first i create my first activity inside that i accessing my database values and trying to activate broadcast receiver.
public class PARENT_CALLActivity extends Activity
{
String PARENT=null;
EditText edparent;
Button submit;
String parent_number;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edparent=(EditText)findViewById(R.id.editText1);
submit=(Button)findViewById(R.id.btnsubmit);
submit.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
PARENT=edparent.getText().toString();
MyDabasehandler db=new MyDabasehandler(getApplicationContext());
if(db.getContact().equals(null))
{
db.addContact(new Contacts(PARENT));
}
else
{
db.editContact();
}
Intent reciver=new Intent(getApplicationContext(),myBroadcast.class);
startActivity(reciver);
}
});
}
}
my broadcast receiver....
public class myBroadcast extends BroadcastReceiver
{
String out_number;
String myparent;
#Override
public void onReceive(Context context, Intent intent)
{
// TODO Auto-generated method stub
out_number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
MyDabasehandler db=new MyDabasehandler(context);
myparent=db.getContact().toString();
//Toast.makeText(context, out_number+"number", Toast.LENGTH_LONG).show();
SmsManager sm=SmsManager.getDefault();
sm.sendTextMessage(myparent, "5554", "calling..to"+out_number, null, null);//5554 is my emulator number to check its in emulator
Toast.makeText(context, "send"+out_number, Toast.LENGTH_SHORT).show();
}
}
and my Manifest is..
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sha.pcall"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"/>
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".PARENT_CALLActivity"
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=".MyDabasehandler"
android:label="#string/app_name">
</activity>
<activity android:name=".Contacts"
android:label="#string/app_name">
</activity>
<receiver android:name=".myBroadcast"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.NEW_OUTGOING_CALL"/>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>
</application>
/manifest>
and i am getting force to close when i press the submit button.
You should put the LogCat output that shows the exception StackTrace.
What I can spot in the code that it seems that you are missing
<uses-permission android:name="android.permission.READ_PHONE_STATE" >

Service not starting

Probably an easy question for you guys, but its my first attempt at creating a service that should run in the background when my app closes. My problem is: The service doesnt start when I click on my "start service" button. I don't see any of the toast, and nothing in the logcat (no errors either). Thanks in advance!
Service class
public class MyService extends Service {
private static final String TAG = "MyService";
#Override
public IBinder onBind(Intent intent) {
return null;
}
#Override
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();
Log.d(TAG, "onCreate");
}
#Override
public void onDestroy() {
Toast.makeText(this, "My Service Stopped", Toast.LENGTH_LONG).show();
Log.d(TAG, "onDestroy");
}
#Override
public void onStart(Intent intent, int startid) {
Toast.makeText(this, "My Service Started", Toast.LENGTH_LONG).show();
Log.d(TAG, "onStart");
}
}
My main activity
public void startServiceClick(View v){
Log.d("foo", "onClick: starting srvice");
startService(new Intent(this, MyService.class));
}
public void stopServiceClick(View v){
Log.d("foo", "onClick: stopping srvice");
stopService(new Intent(this, MyService.class));
}
Manifest xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="se.johanberntsson.main"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.BLUETOOTH"/>
<application
android:icon="#drawable/maps1"
android:label="#string/app_name" >
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".LongitudeActivity"
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=".DebugActivity" />
<activity android:name=".GoogleMapsActivity" />
<service android:enabled="true" android:name=".MyService" />
</application>
</manifest>
this is suggestion as all else looking fine
should call super in each function like super.onCreate();
as may be possible service is already started and try to stop and start again and check it toast appears ....
try
<service android:enabled="true" android:name="se.johanberntsson.servies.MyService" />
You may have already figured this out. In your code you override several native functions so you could add a toast and a catlog entry. Don't forget to add super.{whatever function you are calling}. What that does is it tells the android device to do whatever the function would have done had the function not been overrided, then do the extra stuff. In your case, if you call your oncreate function, it then the computer will make the toast and add a catlog entry, but it will not create the service.
#Override //forget about doing whatever onCreate usually does.
public void onCreate() {
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();//toast
Log.d(TAG, "onCreate");//Add message to cat- log
//note: Service not created because function was overrided
}
however, if you add super.onCreate, the phone will create the service, then it will do what you tell it to do.
#Override //forget about doing whatever onCreate usually does.
public void onCreate() {
super.onCreate(); // do whatever onCreate usually does. Then do whatever is after me.
Toast.makeText(this, "My Service Created", Toast.LENGTH_LONG).show();//toast
Log.d(TAG, "onCreate");//Add message to cat- log
}
I hope this helps.
I added your service into my code and it is working fine for me. please see below code and match with yours.
Activity >>
public class TestActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bt = (Button)findViewById(R.id.button11);
bt.setOnClickListener(this);
}
public void startServiceClick(View v){
Log.d("foo", "onClick: starting srvice");
startService(new Intent(this, MyService.class));
}
public void stopServiceClick(View v){
Log.d("foo", "onClick: stopping srvice");
stopService(new Intent(this, MyService.class));
}
#Override
public void onClick(View v) {
startServiceClick(v);
}
Manifest File >>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".TestActivity"
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:enabled="true" android:name=".MyService" />
</application>

registerMediaButtonEventReceiver / handling volume buttons issue

In my application I need to process physical button events such as volume button clicks. For that I'm using audiomanager's registerMediaButtonEventReceiver method. There's an article relevant to my situation, though I can't get it working.
Here's code I'm using:
public class VolumeBroadcastActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AudioManager manager = (AudioManager) getSystemService(AUDIO_SERVICE);
manager.registerMediaButtonEventReceiver(new ComponentName(getPackageName(), RemoteControlReceiver.class.getName()));
}
public class RemoteControlReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(VolumeBroadcastActivity.this, "1",1).show();
if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="localhost.volume.broadcast"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name=".VolumeBroadcastActivity"
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="RemoteControlReceiver">
<intent-filter>
<action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>
</application>
</manifest>
Your RemoteControlReceiver class is an inner class of VolumeBroadcastActivity. Your manifest doesn't say so and I don't think it could. Make it a regular class and precede its android:name with a dot.
You might need this permission (declare in manifest):
uses-permission android:name="android.permission.BLUETOOTH"

Categories

Resources