簡體   English   中英

如何在Android的撥號器(默認或股票撥號器)中啟動服務

[英]How to start service in dialer (default or stock dialer) of android

我想在我的撥號器(默認或股票撥號器)打開時啟動服務。 我的服務只是祝酒消息。 如何啟動撥號器服務。 startService命令在MainActivity工作,但在撥號器打開時它不起作用。

我的代碼如下所示:

的Manifest.xml

 <?xml version="1.0" encoding="utf-8"?>    

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.chatheads">

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
<uses-permission android:name="android.permission.DIAL_ACTION" />
<uses-permission android:name="android.permission.CALL_PHONE" />


    <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=".MyService"
        android:enabled="true"
        android:exported="true" />

    <activity android:name=".SecondActivity"></activity>
</application>

</manifest> 

MainActivity.java

package com.example.chatheads;

import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.DialerKeyListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;


public class MainActivity extends AppCompatActivity {
Button startService,stopService;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startService=(Button)findViewById(R.id.startService);
    stopService=(Button)findViewById(R.id.stopService);




    if(getIntent().getAction().equals(Intent.ACTION_DIAL)) {
        Intent intent = new Intent(getBaseContext(), MyService.class);
        startService(intent);

    }

    startService.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            startService(new Intent(getApplication(), MyService.class));




        }
    });
    stopService.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            stopService(new Intent(getApplication(), MyService.class));



        }
    });


  }
}

Myservice.java

package com..service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

public class MyService extends Service {
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
    super.onCreate();
    Toast.makeText(this, "Service Created", Toast.LENGTH_LONG).show();

    }
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub
    Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
    intent.getStringExtra("MY_MESSAGE");
    //stopSelf();
    return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Toast.makeText(this, "Service Stopped", Toast.LENGTH_LONG).show();

}

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

}

此代碼無效。 在MainActivity中調用startService時啟動的服務。 但是當撥號器打開它不起作用。

我想也許你可以嘗試將getApplication ()更改為MainActivity.this

我認為您需要從撥號器跳轉到MainActivity以啟動服務

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM