我有一个申请" x"按钮单击启动服务(在其他一些包中)。此服务现在应该读取呼叫日志并将数据发送到应用程序" y"应该在SD卡中写入数据。目前,我的服务按下按钮点击,但应用程序" y"因为我在应用程序中祝酒并且#34; y"它永远不会出现。这些权限用于读取日志,因为我使用自定义操作,因此应用程序的意图过滤器中包含相同的权限#34; y"。有人能说出这个问题的原因。
服务代码
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("TAG", "Service started.");
super.onStartCommand(intent, flags, startId);
StringBuffer sb = new StringBuffer();
String strOrder = android.provider.CallLog.Calls.DATE + " DESC";
/* Query the CallLog Content Provider */
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALL_LOG) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return 0;
}
Cursor managedCursor = getContentResolver().query(CallLog.Calls.CONTENT_URI, null,
null, null, strOrder);
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("Call Log :");
int h = 0;
while (managedCursor.moveToNext() && h != 20) {
String phNum = managedCursor.getString(number);
String callTypeCode = managedCursor.getString(type);
String strcallDate = managedCursor.getString(date);
Date callDate = new Date(Long.valueOf(strcallDate));
String callDuration = managedCursor.getString(duration);
String callType = null;
h++;
int callcode = Integer.parseInt(callTypeCode);
switch (callcode) {
case CallLog.Calls.OUTGOING_TYPE:
callType = "Outgoing";
break;
case CallLog.Calls.INCOMING_TYPE:
callType = "Incoming";
break;
case CallLog.Calls.MISSED_TYPE:
callType = "Missed";
break;
}
sb.append("\nPhone Number:--- " + phNum + " \nCall Type:--- "
+ callType + " \nCall Date:--- " + callDate
+ " \nCall duration in sec :--- " + callDuration);
sb.append("\n----------------------------------");
}
managedCursor.close();
managedCursor.close();
Intent sendIntent = new Intent();
String CUSTOM_ACTION = "com.example.sonali.callreadingservice.CALL_LOGS";
sendIntent.setAction(CUSTOM_ACTION);
sendIntent.putExtra(Intent.EXTRA_TEXT, (Serializable) sb);
sendIntent.setType("text/plain");
startActivity(sendIntent);
this.stopSelf();
return 0;
}
@Override
public void onDestroy() {
Log.d("slog", "onDestroy()");
super.onDestroy();
}
申请" y"主要活动
``public class MainActivity extends AppCompatActivity {
TextView textView = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toast.makeText(this, "Service Started", Toast.LENGTH_SHORT).show();
textView = (TextView) findViewById(R.id.textview_call);
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
handleSendText(intent);
}
void handleSendText(Intent intent) {
String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
try {
if (sharedText != null) {
textView.setText(sharedText);
File sdCard = Environment.getExternalStorageDirectory();
FileWriter writer = new FileWriter(sdCard);
// Writes the content to the file
writer.write(sharedText);
writer.flush();
writer.close();
}
} catch (Exception e) {
}
}
应用程序的意图过滤器" y" `
`<intent-filter>
<action android:name="com.example.sonali.callreadingservice.CALL_LOGS" />
<category android:name="android.intent.category.LAUNCHER" />
<data android:mimeType="text/plain" />
</intent-filter>
答案 0 :(得分:0)
您需要在清单中将自定义操作添加到活动中。
<activity
android:name="MainActivity">
<intent-filter>
<action android:name="com.example.sonali.callreadingservice.CALL_LOGS"/>
</intent-filter>
</activity>