服务没有在android中运行

时间:2014-04-13 12:29:13

标签: android broadcastreceiver bootcompleted

我想制作代码来接收短信,因为我已经制作了一个BroadcastReceiver活动甚至提供了服务。但是,我的服务没有启动。我已经在onStart服务方法上显示了吐司。

这是我的服务类:

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清单文件:

<?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>

我不知道出了什么问题。请帮助您提出建议。

2 个答案:

答案 0 :(得分:0)

关于如何在manifest.xml上添加服务,您可以像<service android:name=".MyService" />一样简单地添加服务

答案 1 :(得分:0)

您应该以最简单的方式注册服务:

<?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>

并像这样使用它:

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);
    }
}

并像这样创建:

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;
    }
}