Service not running in android

I want to make code to receive sms, for that I have made a BroadcastReceiver activity and even made service. But, my service is not starting.I have displayed toast on the onStart method of Service.

This is my Service class:

public class Bg_sms_verify_service extends Service {

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public void onCreate() {
        Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG).show();
    }


     @Override
     public void onStart(Intent intent, int startId) {
             // For time consuming an long tasks you can launch a new thread here...
        Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
       /* BroadcastReceiver_file bc = new BroadcastReceiver_file();
        bc.onReceive(this, intent);*/

    }

}

Android Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.anti_theftapplication"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />
    <uses-permission android:name="android.permission.SEND_SMS"/>
    <uses-permission android:name="android.permission.RECEIVE_SMS"></uses-permission>
    <uses-permission android:name="android.permission.READ_SMS" />
    <uses-permission android:name="android.permission.SEND_SMS"></uses-permission>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.anti_theftapplication.MainActivity"
            android:label="@string/app_name" >

        </activity>
        <activity
            android:name="com.example.anti_theftapplication.ListOfOptions"
            android:label="@string/title_activity_list_of_options" >

             <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>
        <activity
            android:name="com.example.anti_theftapplication.SMS_Send"
            android:label="@string/title_activity_sms__send" >

        </activity>
         <receiver android:name="com.androidexample.anti_theftapplication.BroadcastReceiver_file" android:enabled="true" android:exported="false">   
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
                <action android:name="android.intent.action.BOOT_COMPLETED"/>
            </intent-filter>
        </receiver>


    </application>

</manifest>

I don't know what is wrong. Please help with your suggestions.

Answers


You should register services in simplest way like this:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.internalexternalstorageexample"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="19" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".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>

        <service android:name=".DownloadHtmlService" >
        </service>
    </application>

</manifest>

and use it like this:

package com.example.internalexternalstorageexample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(MainActivity.this, DownloadHtmlService.class);
        intent.putExtra("url", "http://www.playboyplus.com/gallery/15433/christine-carter-fresh-beauty-nude/");
        startService(intent);
    }
}

and create it like this:

package com.example.internalexternalstorageexample;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Service;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;
public class DownloadHtmlService extends Service {

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        Toast.makeText(this, "The new Service was Created", Toast.LENGTH_LONG)
                .show();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        // TODO Auto-generated method stub
        super.onStart(intent, startId);
    }

    @Override
    public int onStartCommand(final Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                URL url;
                ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
                NetworkInfo info = manager.getActiveNetworkInfo();
                if (info != null && info.isConnected()) {
                    try {
                        url = new URL(intent.getStringExtra("url"));
                        HttpURLConnection con = (HttpURLConnection) url
                                .openConnection();
                        con.setRequestMethod("GET");
                        con.setDoInput(true);
                        con.setConnectTimeout(15000);
                        con.connect();
                        int responsecode = con.getResponseCode();
                        if (responsecode == 200) {
                            InputStream in = con.getInputStream();
                            BufferedReader reader = new BufferedReader(
                                    new InputStreamReader(in));
                            String s = "", s1 = "";
                            while ((s = reader.readLine()) != null) {
                                s1 = s1 + s;
                            }
                            Log.e("balvier", s1);

                        }
                    } catch (Exception e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }).start();

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public IBinder onBind(Intent arg0) {
        // TODO Auto-generated method stub
        return null;
    }
}

regarding on how to add your service on manifest.xml you can add your service as simple as <service android:name=".MyService" />


Need Your Help

How to eliminate for each loop?

.net vb.net

I am already using following code to get EXCEL.EXE starting time. (Following code is okey)

render to floating point texture under iOS

iphone opengl-es

The iPad now supports the OES_texture_half_float